[llvm] [AMDGPU] Call the `FINI_ARRAY` destructors in the correct order (PR #71815)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 10 08:37:13 PST 2023


================
@@ -96,15 +105,39 @@ static void createInitOrFiniCalls(Function &F, bool IsCtor) {
   // for now we just call them with no arguments.
   auto *CallBackTy = FunctionType::get(IRB.getVoidTy(), {});
 
-  IRB.CreateCondBr(IRB.CreateICmpNE(Begin, End), LoopBB, ExitBB);
+  Constant *Start = Begin;
+  Constant *Stop = End;
+  // The destructor array must be called in reverse order. Get a constant
+  // expression to the end of the array and iterate backwards instead.
+  if (!IsCtor) {
+    Type *Int64Ty = IntegerType::getInt64Ty(C);
+    auto *Offset = ConstantExpr::getSub(
+        ConstantExpr::getAShr(
+            ConstantExpr::getSub(ConstantExpr::getPtrToInt(End, Int64Ty),
+                                 ConstantExpr::getPtrToInt(Begin, Int64Ty)),
+            ConstantInt::get(Int64Ty, 3)),
+        ConstantInt::get(Int64Ty, 1));
+    Start = ConstantExpr::getGetElementPtr(
+        ArrayType::get(IRB.getPtrTy(), 0), Begin,
+        ArrayRef<Constant *>({ConstantInt::get(Int64Ty, 0), Offset}),
+        /*InBounds=*/true);
+    Stop = Begin;
----------------
nikic wrote:

Context is https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179. You had some unlucky timing here in that you landed this patch just as I was putting up https://github.com/llvm/llvm-project/pull/71955, which removes `ConstantExpr::getAShr()` entirely.

The godbolt output is outdated. After https://github.com/llvm/llvm-project/commit/82f68a992b9f89036042d57a5f6345cb2925b2c1 clang produces https://gist.github.com/nikic/55e38138d0ff09f0ba2c08bc23ea3e86 instead.

And no, you can't replace this with division by eight. Division has not been supported in constant expressions for a while already. All you really need to do is replace the constant expressions generated here with IRBuilder calls. IRBuilder will generate instructions or constant expressions as appropriate.

https://github.com/llvm/llvm-project/pull/71815


More information about the llvm-commits mailing list