[PATCH] D149340: [AMDGPU] Place global constructors in .init_array and .fini_array

Joseph Huber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 27 08:19:03 PDT 2023


jhuber6 updated this revision to Diff 517569.
jhuber6 added a comment.

Rebase


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D149340/new/

https://reviews.llvm.org/D149340

Files:
  llvm/lib/Target/AMDGPU/AMDGPUCtorDtorLowering.cpp
  llvm/test/CodeGen/AMDGPU/lower-ctor-dtor.ll


Index: llvm/test/CodeGen/AMDGPU/lower-ctor-dtor.ll
===================================================================
--- llvm/test/CodeGen/AMDGPU/lower-ctor-dtor.ll
+++ llvm/test/CodeGen/AMDGPU/lower-ctor-dtor.ll
@@ -4,12 +4,16 @@
 ; Make sure we get the same result if we run multiple times
 ; RUN: opt -S -mtriple=amdgcn-- -passes=amdgpu-lower-ctor-dtor,amdgpu-lower-ctor-dtor < %s | FileCheck %s
 ; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx700 -filetype=obj -o - < %s | llvm-readelf -s - 2>&1 | FileCheck %s -check-prefix=VISIBILITY
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx700 -filetype=obj -o - < %s | llvm-readelf -S - 2>&1 | FileCheck %s -check-prefix=SECTION
 
 @llvm.global_ctors = appending addrspace(1) global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 1, ptr @foo, ptr null }]
 @llvm.global_dtors = appending addrspace(1) global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 1, ptr @bar, ptr null }]
 
 ; CHECK-NOT: @llvm.global_ctors
 ; CHECK-NOT: @llvm.global_dtors
+; CHECK: @foo.init = internal constant ptr @foo, section ".init_array.1"
+; CHECK: @bar.fini = internal constant ptr @bar, section ".fini_array.1"
+; CHECK: @llvm.used = appending global [4 x ptr] [ptr @foo.init, ptr @amdgcn.device.init, ptr @bar.fini, ptr @amdgcn.device.fini]
 
 ; CHECK-LABEL: amdgpu_kernel void @amdgcn.device.init() #0
 ; CHECK-NEXT: call void @foo
@@ -25,6 +29,8 @@
 ; VISIBILITY: OBJECT GLOBAL DEFAULT {{.*}} amdgcn.device.init.kd
 ; VISIBILITY: FUNC   GLOBAL PROTECTED {{.*}} amdgcn.device.fini
 ; VISIBILITY: OBJECT   GLOBAL DEFAULT {{.*}} amdgcn.device.fini.kd
+; SECTION: .init_array.1     INIT_ARRAY      {{.*}} {{.*}} 000008 00  WA  0   0  8
+; SECTION: .fini_array.1     FINI_ARRAY      {{.*}} {{.*}} 000008 00  WA  0   0  8
 
 define internal void @foo() {
   ret void
Index: llvm/lib/Target/AMDGPU/AMDGPUCtorDtorLowering.cpp
===================================================================
--- llvm/lib/Target/AMDGPU/AMDGPUCtorDtorLowering.cpp
+++ llvm/lib/Target/AMDGPU/AMDGPUCtorDtorLowering.cpp
@@ -63,6 +63,18 @@
 
   for (Value *V : GA->operands()) {
     auto *CS = cast<ConstantStruct>(V);
+    if (auto *F = dyn_cast<Function>(CS->getOperand(1))) {
+      uint64_t Priority = cast<ConstantInt>(CS->getOperand(0))->getSExtValue();
+      std::string PriorityStr = "." + std::to_string(Priority);
+      auto *GV = new GlobalVariable(
+          M, F->getType(), /*IsConstant=*/true, GlobalValue::InternalLinkage, F,
+          F->getName() + (IsCtor ? ".init" : ".fini"));
+      llvm::errs() << Priority << "\n";
+      GV->setSection(IsCtor ? ".init_array" + PriorityStr
+                            : ".fini_array" + PriorityStr);
+      appendToUsed(M, {GV});
+      llvm::errs() << Priority << " " << *GV << "\n";
+    }
     IRB.CreateCall(ConstructorTy, CS->getOperand(1));
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149340.517569.patch
Type: text/x-patch
Size: 2833 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230427/03fb3337/attachment.bin>


More information about the llvm-commits mailing list