-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
[Clang] Define AMDGPU ABI when referenced in CodeGen for ABI "none" #66162
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
llvmbot
added
clang
Clang issues not falling into any other category
backend:AMDGPU
clang:codegen
labels
Sep 13, 2023
@llvm/pr-subscribers-backend-amdgpu @llvm/pr-subscribers-clang-codegen ChangesSummary: We use the 1llvm.amgcn.abi.version` varaible to control code generation. This is emitted in every module now to indicate what should be used when compiling. Previously, the logic caused us to emit an external reference to this variable when creating the code for the `none` type. This would then cause us not to emit the actual definition. This patch refines the logic to create the external reference, and then update it if it is found unset by the time we emit the global. I had to remove the reference to `GetOrCreateLLVmGlobal` because it did not accept the proper address space.-- 3 Files Affected:
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 8b19bf85d47a19f..fcda6e67934330a 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -17188,9 +17188,14 @@ Value *EmitAMDGPUWorkGroupSize(CodeGenFunction &CGF, unsigned Index) { auto Cov = CGF.getTarget().getTargetOpts().CodeObjectVersion; if (Cov == clang::TargetOptions::COV_None) { - auto *ABIVersionC = CGF.CGM.GetOrCreateLLVMGlobal( - "llvm.amdgcn.abi.version", CGF.Int32Ty, LangAS::Default, nullptr, - CodeGen::NotForDefinition); + StringRef Name = "llvm.amdgcn.abi.version"; + auto *ABIVersionC = CGF.CGM.getModule().getNamedGlobal(Name); + if (!ABIVersionC) + ABIVersionC = new llvm::GlobalVariable( + CGF.CGM.getModule(), CGF.Int32Ty, false, + llvm::GlobalValue::ExternalLinkage, nullptr, Name, nullptr, + llvm::GlobalVariable::NotThreadLocal, + CGF.CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant)); // This load will be eliminated by the IPSCCP because it is constant // weak_odr without externally_initialized. Either changing it to weak or diff --git a/clang/lib/CodeGen/Targets/AMDGPU.cpp b/clang/lib/CodeGen/Targets/AMDGPU.cpp index c168bd4b7c7cc15..26a9ab812e96c98 100644 --- a/clang/lib/CodeGen/Targets/AMDGPU.cpp +++ b/clang/lib/CodeGen/Targets/AMDGPU.cpp @@ -362,7 +362,8 @@ void AMDGPUTargetCodeGenInfo::setFunctionDeclAttributes( void AMDGPUTargetCodeGenInfo::emitTargetGlobals( CodeGen::CodeGenModule &CGM) const { StringRef Name = "llvm.amdgcn.abi.version"; - if (CGM.getModule().getNamedGlobal(Name)) + llvm::GlobalVariable *OriginalGV = CGM.getModule().getNamedGlobal(Name); + if (!llvm::GlobalVariable::isExternalLinkage(OriginalGV->getLinkage())) return; auto *Type = llvm::IntegerType::getIntNTy(CGM.getModule().getContext(), 32); @@ -377,6 +378,13 @@ void AMDGPUTargetCodeGenInfo::emitTargetGlobals( CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant)); GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Local); GV->setVisibility(llvm::GlobalValue::VisibilityTypes::HiddenVisibility); + + // Replace any external references to this variable with the new global. + if (OriginalGV) { + OriginalGV->replaceAllUsesWith(GV); + GV->takeName(OriginalGV); + OriginalGV->eraseFromParent(); + } } void AMDGPUTargetCodeGenInfo::setTargetAttributes( diff --git a/clang/test/CodeGen/amdgpu-abi-version.c b/clang/test/CodeGen/amdgpu-abi-version.c new file mode 100644 index 000000000000000..1cec45fb213057c --- /dev/null +++ b/clang/test/CodeGen/amdgpu-abi-version.c @@ -0,0 +1,32 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-globals --version 3 +// RUN: %clang_cc1 -cc1 -triple amdgcn-amd-amdhsa -emit-llvm -mcode-object-version=none %s -o - | FileCheck %s + +//. +// CHECK: @llvm.amdgcn.abi.version = weak_odr hidden local_unnamed_addr addrspace(4) constant i32 0 +//. +// CHECK-LABEL: define dso_local i32 @foo( +// CHECK-SAME: ) #[[ATTR0:[0-9]+]] { +// CHECK-NEXT: entry: +// CHECK-NEXT: [[RETVAL:%.*]] = alloca i32, align 4, addrspace(5) +// CHECK-NEXT: [[RETVAL_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[RETVAL]] to ptr +// CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr addrspace(4) @llvm.amdgcn.abi.version, align 4 +// CHECK-NEXT: [[TMP1:%.*]] = icmp sge i32 [[TMP0]], 500 +// CHECK-NEXT: [[TMP2:%.*]] = call align 8 dereferenceable(256) ptr addrspace(4) @llvm.amdgcn.implicitarg.ptr() +// CHECK-NEXT: [[TMP3:%.*]] = getelementptr i8, ptr addrspace(4) [[TMP2]], i32 12 +// CHECK-NEXT: [[TMP4:%.*]] = call align 4 dereferenceable(64) ptr addrspace(4) @llvm.amdgcn.dispatch.ptr() +// CHECK-NEXT: [[TMP5:%.*]] = getelementptr i8, ptr addrspace(4) [[TMP4]], i32 4 +// CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP1]], ptr addrspace(4) [[TMP3]], ptr addrspace(4) [[TMP5]] +// CHECK-NEXT: [[TMP7:%.*]] = load i16, ptr addrspace(4) [[TMP6]], align 2, !range [[RNG2:![0-9]+]], !invariant.load !3, !noundef !3 +// CHECK-NEXT: [[CONV:%.*]] = zext i16 [[TMP7]] to i32 +// CHECK-NEXT: ret i32 [[CONV]] +// +int foo() { return __builtin_amdgcn_workgroup_size_x(); } +//. +// CHECK: attributes #0 = { noinline nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" } +// CHECK: attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) } +//. +// CHECK: !0 = !{i32 1, !"wchar_size", i32 4} +// CHECK: !1 = !{!"clang version 18.0.0"} +// CHECK: !2 = !{i16 1, i16 1025} +// CHECK: !3 = !{} +//. |
jhuber6
force-pushed
the
FixABIVersion
branch
2 times, most recently
from
September 13, 2023 02:10
a78d2d9
to
afe7458
Compare
saiislam
approved these changes
Sep 13, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Summary: We use the 1llvm.amgcn.abi.version` varaible to control code generation. This is emitted in every module now to indicate what should be used when compiling. Previously, the logic caused us to emit an external reference to this variable when creating the code for the `none` type. This would then cause us not to emit the actual definition. This patch refines the logic to create the external reference, and then update it if it is found unset by the time we emit the global. I had to remove the reference to `GetOrCreateLLVmGlobal` because it did not accept the proper address space.
jhuber6
force-pushed
the
FixABIVersion
branch
from
September 13, 2023 13:11
afe7458
to
1aab904
Compare
ZijunZhaoCCK
pushed a commit
to ZijunZhaoCCK/llvm-project
that referenced
this pull request
Sep 19, 2023
…lvm#66162) Summary: We use the `llvm.amgcn.abi.version` varaible to control code generation. This is emitted in every module now to indicate what should be used when compiling. Previously, the logic caused us to emit an external reference to this variable when creating the code for the `none` type. This would then cause us not to emit the actual definition. This patch refines the logic to create the external reference, and then update it if it is found unset by the time we emit the global. I had to remove the reference to `GetOrCreateLLVmGlobal` because it did not accept the proper address space.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary:
We use the
llvm.amgcn.abi.version
varaible to control code generation.This is emitted in every module now to indicate what should be used when
compiling. Previously, the logic caused us to emit an external reference
to this variable when creating the code for the
none
type. This wouldthen cause us not to emit the actual definition. This patch refines the
logic to create the external reference, and then update it if it is
found unset by the time we emit the global. I had to remove the
reference to
GetOrCreateLLVmGlobal
because it did not accept theproper address space.