Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[DirectX] Fix GEP flattening with 0-indexed GEPs on global variables#149211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

Icohedron
Copy link
Contributor

Fixes#149179

The issue is thatBuilder.CreateGEP does not return a GEP Instruction or GEP ContantExpr when the pointer operand is a global variable and all indices are constant zeroes.

This PR ensures that a GEP instruction is created ifBuilder.CreateGEP did not return a GEP.

@llvmbot
Copy link
Member

@llvm/pr-subscribers-backend-directx

Author: Deric C. (Icohedron)

Changes

Fixes #149179

The issue is thatBuilder.CreateGEP does not return a GEP Instruction or GEP ContantExpr when the pointer operand is a global variable and all indices are constant zeroes.

This PR ensures that a GEP instruction is created ifBuilder.CreateGEP did not return a GEP.


Full diff:https://github.com/llvm/llvm-project/pull/149211.diff

2 Files Affected:

  • (modified) llvm/lib/Target/DirectX/DXILFlattenArrays.cpp (+10)
  • (modified) llvm/test/CodeGen/DirectX/flatten-array.ll (+22)
diff --git a/llvm/lib/Target/DirectX/DXILFlattenArrays.cpp b/llvm/lib/Target/DirectX/DXILFlattenArrays.cppindex ce43645d005b0..f0e2e786dfaf4 100644--- a/llvm/lib/Target/DirectX/DXILFlattenArrays.cpp+++ b/llvm/lib/Target/DirectX/DXILFlattenArrays.cpp@@ -343,6 +343,16 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInst(GetElementPtrInst &GEP) {         Info.RootFlattenedArrayType, Info.RootPointerOperand,         {ZeroIndex, FlattenedIndex}, GEP.getName(), GEP.getNoWrapFlags());+    // If the pointer operand is a global variable and all indices are 0,+    // IRBuilder::CreateGEP will return the global variable instead of creating+    // a GEP instruction or GEP ConstantExpr. In this case we have to create and+    // insert our own GEP instruction.+    if (!isa<GEPOperator>(NewGEP))+      NewGEP = GetElementPtrInst::Create(+          Info.RootFlattenedArrayType, Info.RootPointerOperand,+          {ZeroIndex, FlattenedIndex}, GEP.getNoWrapFlags(), GEP.getName(),+          Builder.GetInsertPoint());+     // Replace the current GEP with the new GEP. Store GEPInfo into the map     // for later use in case this GEP was not the end of the chain     GEPChainInfoMap.insert({cast<GEPOperator>(NewGEP), std::move(Info)});diff --git a/llvm/test/CodeGen/DirectX/flatten-array.ll b/llvm/test/CodeGen/DirectX/flatten-array.llindex 1376a1db25975..a2e105537ab88 100644--- a/llvm/test/CodeGen/DirectX/flatten-array.ll+++ b/llvm/test/CodeGen/DirectX/flatten-array.ll@@ -218,6 +218,28 @@ define void @two_index_gep_const() {   ret void }+define void @zero_index_global() {+  ; CHECK-LABEL: define void @zero_index_global(+  ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw [4 x float], ptr addrspace(3) @g.1dim, i32 0, i32 0+  ; CHECK-NEXT: load float, ptr addrspace(3) [[GEP]], align 4+  ; CHECK-NEXT: ret void+  %1 = getelementptr inbounds nuw [2 x [2 x float]], ptr addrspace(3) @g, i32 0, i32 0, i32 0+  %2 = load float, ptr addrspace(3) %1, align 4+  ret void+}++; Note: A ConstantExpr GEP with all 0 indices is equivalent to the pointer+; operand of the GEP. Therefore the visitLoadInst will not see the pointer operand+; as a ConstantExpr GEP and will not create a GEP instruction to be visited.+; The later dxil-legalize pass will insert a GEP in this instance.+define void @zero_index_global_const() {+  ; CHECK-LABEL: define void @zero_index_global_const(+  ; CHECK-NEXT: load float, ptr addrspace(3) @g.1dim, align 4+  ; CHECK-NEXT: ret void+  %1 = load float, ptr addrspace(3) getelementptr inbounds nuw ([2 x [2 x float]], ptr addrspace(3) @g, i32 0, i32 0, i32 0), align 4+  ret void+}+ define void @gep_4d_index_test()  {     ; CHECK-LABEL: gep_4d_index_test     ; CHECK: [[a:%.*]] = alloca [16 x i32], align 4

Copy link
Contributor

@bob80905bob80905 left a comment

Choose a reason for hiding this comment

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

LGTM
For my own understanding, in both test cases you added, after flattening, the getelementptr instruction / operand wouldn't be present without this change?

@Icohedron
Copy link
ContributorAuthor

LGTM For my own understanding, in both test cases you added, after flattening, the getelementptr instruction / operand wouldn't be present without this change?

Without this PR the compiler would just crash oncast<GEPOperator>(NewGEP) becauseNewGEP created fromBuilder.CreateGEP in those instances are not GEPs.

bob80905 reacted with thumbs up emoji

// IRBuilder::CreateGEP will return the global variable instead of creating
// a GEP instruction or GEP ConstantExpr. In this case we have to create and
// insert our own GEP instruction.
if (!isa<GEPOperator>(NewGEP))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there something preventing us from just doing this directly the first time?

Copy link
ContributorAuthor

@IcohedronIcohedronJul 17, 2025
edited
Loading

Choose a reason for hiding this comment

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

If I wanted to always create a GEP Instruction, then sure we don't need this if statement and can just useGetElementPtrInst::Create without using the IRBuilder.
But I am keeping the ability to letIRBuilder::CreateGEP create GEP ConstantExprs as well.

@IcohedronIcohedron merged commitfae8df2 intollvm:mainJul 17, 2025
12 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@bob80905bob80905bob80905 approved these changes

@inbelicinbelicinbelic approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

[DirectX] Crash in DXILFlattenArrays.cpp in visitGetElementPtrInst
4 participants
@Icohedron@llvmbot@bob80905@inbelic

[8]ページ先頭

©2009-2025 Movatter.jp