Skip to content

Drop undef/poison initializers for global variables in SPIR-V translation#3661

Open
aobolensk wants to merge 1 commit intoKhronosGroup:mainfrom
aobolensk:undef-to-poison
Open

Drop undef/poison initializers for global variables in SPIR-V translation#3661
aobolensk wants to merge 1 commit intoKhronosGroup:mainfrom
aobolensk:undef-to-poison

Conversation

@aobolensk
Copy link
Copy Markdown
Contributor

@aobolensk aobolensk commented Mar 17, 2026

Align the translator with the LLVM SPIR-V backend behavior : undef/poison initializers are dropped unless the variable is constant with an aggregate type. This avoids emitting unnecessary OpUndef initializers in the generated SPIR-V.

@aobolensk
Copy link
Copy Markdown
Contributor Author

@MrSidims please, take a look

Copy link
Copy Markdown
Contributor

@vmaksimo vmaksimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I seriously doubt we should make this change before LLVM totally removes undef.
We already aligned to the community wherever possible (cec12d6) in terms of using poison, but translation of OpUndef to poison does not seem reasonable to me right now.

@aobolensk
Copy link
Copy Markdown
Contributor Author

I seriously doubt we should make this change before LLVM totally removes undef. We already aligned to the community wherever possible (cec12d6) in terms of using poison, but translation of OpUndef to poison does not seem reasonable to me right now.

Actually, that was done now for the purpose of alignment with SPIR-V backend in LLVM

@MrSidims
Copy link
Copy Markdown
Contributor

I seriously doubt we should make this change before LLVM totally removes undef.

LLVM is not going to remove undef.

Actually, that was done now for the purpose of alignment with SPIR-V backend in LLVM

Do we understand, why SPIR-V module produced by the backend results in poison creation after reverse translation when SPIR-V module produced by the translator is resulting in undef after the round trip?


case OpUndef:
return mapValue(BV, UndefValue::get(transType(BV->getType())));
return mapValue(BV, PoisonValue::get(transType(BV->getType())));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't do the replacement unconditionally. If we really want to invest into this, then we should do this following https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_validity_and_defined_behavior aka check which instruction generates/consume OpUndef and if consuming this undef/poison value is forbidden for an instruction - undef should be generated in LLVM IR, and poison otherwise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Shall we follow the logic described in the spec directly? As I can see, there could be several "forbidden consumers" such as conditional branch instructions. Or are there any other assumptions? The check seems to be very non-elegant, because it tries to evaluate the context of the users (other mappers here do not perform such checks)

Copy link
Copy Markdown
Contributor

@MrSidims MrSidims Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct me if I'm wrong. The patch addresses this fixme, right? If so, I would like to understand, why roundtrip through llvm-spirv alone and through backend + llvm-spirv are different before suggesting an approach.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point is that backend omits custom "undef" initializers, because they don't make sense. Updated the patch to focus on this specific situation along with PR title & description

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MrSidims please, take a look at the updated approach

@aobolensk aobolensk requested a review from MrSidims March 18, 2026 13:53
@aobolensk
Copy link
Copy Markdown
Contributor Author

Do we understand, why SPIR-V module produced by the backend results in poison creation after reverse translation when SPIR-V module produced by the translator is resulting in undef after the round trip?

Backend does not produce OpUndef by itself, while translator does. That creates the difference

@aobolensk aobolensk changed the title Translate OpUndef to poison instead of undef in SPIR-V reader Drop undef/poison initializers for global variables in SPIR-V translation Mar 24, 2026
@aobolensk aobolensk force-pushed the undef-to-poison branch 2 times, most recently from 0e198c7 to fa057ab Compare March 27, 2026 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants