Skip to content

Inline: Fix handling of byval using non-alloca addrspace #97306

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Jul 1, 2024

Use the address space of the original pointer argument instead
of querying the datalayout. This avoids producing a verifier error
since this was changing the address space for the user instructions.

Fixes #97086

Copy link
Contributor Author

arsenm commented Jul 1, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @arsenm and the rest of your teammates on Graphite Graphite

@arsenm arsenm added the ipo Interprocedural optimizations label Jul 1, 2024 — with Graphite App
@arsenm arsenm marked this pull request as ready for review July 1, 2024 15:04
@llvmbot
Copy link
Member

llvmbot commented Jul 1, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Matt Arsenault (arsenm)

Changes

Use the address space of the original pointer argument instead
of querying the datalayout. This avoids producing a verifier error
since this was changing the address space for the user instructions.

Fixes #97086


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/InlineFunction.cpp (+3-2)
  • (added) llvm/test/Transforms/Inline/byval-with-non-alloca-addrspace.ll (+38)
diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp
index 0725addfbb90a..46605492490ea 100644
--- a/llvm/lib/Transforms/Utils/InlineFunction.cpp
+++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp
@@ -1674,8 +1674,9 @@ static Value *HandleByValArgument(Type *ByValType, Value *Arg,
   if (ByValAlignment)
     Alignment = std::max(Alignment, *ByValAlignment);
 
-  AllocaInst *NewAlloca = new AllocaInst(ByValType, DL.getAllocaAddrSpace(),
-                                         nullptr, Alignment, Arg->getName());
+  AllocaInst *NewAlloca = new AllocaInst(
+      ByValType, cast<PointerType>(Arg->getType())->getAddressSpace(), nullptr,
+      Alignment, Arg->getName());
   NewAlloca->insertBefore(Caller->begin()->begin());
   IFI.StaticAllocas.push_back(NewAlloca);
 
diff --git a/llvm/test/Transforms/Inline/byval-with-non-alloca-addrspace.ll b/llvm/test/Transforms/Inline/byval-with-non-alloca-addrspace.ll
new file mode 100644
index 0000000000000..42ec0f2bf5699
--- /dev/null
+++ b/llvm/test/Transforms/Inline/byval-with-non-alloca-addrspace.ll
@@ -0,0 +1,38 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=always-inline %s | FileCheck %s
+
+; The byval parameters use a different address space from the alloca
+; address space. Make sure this is gracefully handled by using the
+; original byval pointer type's address space instead of what the
+; datalayout says to use.
+
+target datalayout = "A5"
+
+%struct = type { i64, i64 }
+
+define i64 @bar(ptr byval(%struct) %a) alwaysinline {
+; CHECK-LABEL: define i64 @bar(
+; CHECK-SAME: ptr byval([[STRUCT:%.*]]) [[A:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr [[STRUCT]], ptr [[A]], i64 0, i32 1
+; CHECK-NEXT:    [[TMP2:%.*]] = load i64, ptr [[TMP1]], align 4
+; CHECK-NEXT:    ret i64 0
+;
+  %1 = getelementptr %struct, ptr %a, i64 0, i32 1
+  %2 = load i64, ptr %1, align 4
+  ret i64 0
+}
+
+define i64 @foo(ptr %arg) {
+; CHECK-LABEL: define i64 @foo(
+; CHECK-SAME: ptr [[ARG:%.*]]) {
+; CHECK-NEXT:    [[ARG1:%.*]] = alloca [[STRUCT:%.*]], align 8
+; CHECK-NEXT:    call void @llvm.lifetime.start.p0(i64 16, ptr [[ARG1]])
+; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i64(ptr align 1 [[ARG1]], ptr align 1 [[ARG]], i64 16, i1 false)
+; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr [[STRUCT]], ptr [[ARG1]], i64 0, i32 1
+; CHECK-NEXT:    [[TMP2:%.*]] = load i64, ptr [[TMP1]], align 4
+; CHECK-NEXT:    call void @llvm.lifetime.end.p0(i64 16, ptr [[ARG1]])
+; CHECK-NEXT:    ret i64 0
+;
+  %1 = call i64 @bar(ptr byval(%struct) align 8 %arg)
+  ret i64 0
+}

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

github-actions bot commented Jul 1, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

arsenm and others added 3 commits July 1, 2024 20:04
Use the address space of the original pointer argument instead
of querying the datalayout. This avoids producing a verifier error
since this was changing the address space for the user instructions.

Fixes #97086
Co-authored-by: Nikita Popov <[email protected]>
@arsenm arsenm force-pushed the users/arsenm/issue97086-inliner-byval-non-alloca-addrspace branch from c4d62fc to 4cc0aa4 Compare July 1, 2024 18:05
@arsenm arsenm merged commit e47359a into main Jul 1, 2024
5 of 6 checks passed
@arsenm arsenm deleted the users/arsenm/issue97086-inliner-byval-non-alloca-addrspace branch July 1, 2024 19:09
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
Use the address space of the original pointer argument instead
of querying the datalayout. This avoids producing a verifier error
since this was changing the address space for the user instructions.

Fixes llvm#97086
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
Use the address space of the original pointer argument instead
of querying the datalayout. This avoids producing a verifier error
since this was changing the address space for the user instructions.

Fixes llvm#97086
@@ -1674,8 +1674,9 @@ static Value *HandleByValArgument(Type *ByValType, Value *Arg,
if (ByValAlignment)
Alignment = std::max(Alignment, *ByValAlignment);

AllocaInst *NewAlloca = new AllocaInst(ByValType, DL.getAllocaAddrSpace(),
Copy link
Contributor

@MrSidims MrSidims Jul 16, 2024

Choose a reason for hiding this comment

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

@arsenm
Digging a bit into git log the patch that added DL.getAllocaAddrSpace() was the following:

commit 3c1fc768ed6cf2b01463df036ae6ae3b1f0de632
Author: Matt Arsenault <[email protected]>
Date:   Mon Apr 10 22:27:50 2017 +0000

    Allow DataLayout to specify addrspace for allocas.

and reasoning behind that patch:
" These are problems for AMDGPU because alloca is used to
implement the private address space, which uses a 32-bit
index as the pointer value. Other pointers are 64-bit
and behave more like LLVM's notion of generic address
space. By changing the address space used for allocas,
we can change our generic pointer type to be LLVM's generic
pointer type which does have similar properties."

makes sense to me especially in OpenCL context, where default address space is 4 (generic). Also IMHO LLVM passes must respect target's datalayout when picking possible types for the created instructions.

Current fix brings some complications though (from OpenCL point of view, but there might be issues for other languages and targets outside of my expertise), if byval pointer of a function was in default aka genetic address space, then the alloca would be in generic. Meanwhile per SPIR-V (to which LLVM IR is being lowered starting from OpenCL 2.0) specification OpVariable must have non-generic storage class. It can probably be workarounded in https://github.com/KhronosGroup/SPIRV-LLVM-Translator by translating alloca addrspace(4) to OpVariable with Private storage class bringing extra address space casts, or by modifying clang, introducing an exception for byval pointers making them not be in generic address space, but in private, but I wonder if #97086 can be solved differently - instead here if Arg->getType()->getPointerAddressSpace() != DL.getAllocaAddrSpace() - we still create an alloca in default datalayout address space and add an AS cast, WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

instead here if Arg->getType()->getPointerAddressSpace() != DL.getAllocaAddrSpace() - we still create an alloca in default datalayout address space and add an AS cast, WDYT?

I'm not sure I fully understand the proposal, but for the CHERI use case (the AMD version was based on the CHERI fork, which we aim to start upstreaming soon), allocas must be created in the alloca address space, not some other address space and cast.

Copy link
Contributor

@MrSidims MrSidims Jul 16, 2024

Choose a reason for hiding this comment

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

I mean: leave alloca in DL.getAllocaAddrSpace() AS and if it doesn't match with byval pointer's address space - create a cast from alloca to byval's address space and update users of the alloca to use the cast. But digging more into it might be not as trivial as I though initially - at least it seems it shouldn't be done in HandleByValArgument, but probably later in the stack.

Copy link
Contributor

Choose a reason for hiding this comment

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

That would not break anything for CHERI, if it works for other use cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this is the correct fix. We should follow what the existing IR says. The rule is not all allocas must have the alloca address space, it's the alloca address space is the default for contextless new values.

As far as OpenCL is concerned such IR would have been malformed in the first place. If we wanted to handle this as a generic pointer with a private backed alloca, it should be handled by lowering the alloca with the wrong address space to include the cast (which is essentially what NVPTX does, it's just more work for the backend and less optimizable IR)

MrSidims added a commit to MrSidims/SPIRV-LLVM-Translator that referenced this pull request Jul 17, 2024
While clang is generating alloca in private address space for OpenCL
following target's datalayout, some of the passes might generate it
in different generic AS and for this cases we have to create OpVariable
in function storage class and create a cast to generic. Additionally
if such alloca was an operand of lifetime instruction - then SPIR-V
without storage class adjustment validation for lifetime instruction
will be violated (pointer operand must be with Function storage class).

This became exposed after llvm/llvm-project#97306
which was fixing handling of byval pointer parameter of a function
during inlining, byval pointer for OpenCL will be generated in default
(under an option - generic) address space making the alloca to store
value of the function's operand also be generic.

Signed-off-by: Sidorov, Dmitry <[email protected]>
MrSidims added a commit to KhronosGroup/SPIRV-LLVM-Translator that referenced this pull request Jul 18, 2024
…2650)

While clang is generating alloca in private address space for OpenCL
following target's datalayout, some of the passes might generate it
in different generic AS and for this cases we have to create OpVariable
in function storage class and create a cast to generic. Additionally
if such alloca was an operand of lifetime instruction - then SPIR-V
without storage class adjustment validation for lifetime instruction
will be violated (pointer operand must be with Function storage class).

This became exposed after llvm/llvm-project#97306
which was fixing handling of byval pointer parameter of a function
during inlining, byval pointer for OpenCL will be generated in default
(under an option - generic) address space making the alloca to store
value of the function's operand also be generic.

Signed-off-by: Sidorov, Dmitry <[email protected]>
iclsrc pushed a commit to intel/llvm that referenced this pull request Jul 26, 2024
…2650)

While clang is generating alloca in private address space for OpenCL
following target's datalayout, some of the passes might generate it
in different generic AS and for this cases we have to create OpVariable
in function storage class and create a cast to generic. Additionally
if such alloca was an operand of lifetime instruction - then SPIR-V
without storage class adjustment validation for lifetime instruction
will be violated (pointer operand must be with Function storage class).

This became exposed after llvm/llvm-project#97306
which was fixing handling of byval pointer parameter of a function
during inlining, byval pointer for OpenCL will be generated in default
(under an option - generic) address space making the alloca to store
value of the function's operand also be generic.

Signed-off-by: Sidorov, Dmitry <[email protected]>

Original commit:
KhronosGroup/SPIRV-LLVM-Translator@f0d7e476924b8f9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ipo Interprocedural optimizations llvm:transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[AMDGPU] [NVPTX] Infer Address Spaces pass leaving behind GEP with no operands
5 participants