[Mlir-commits] [mlir] [OpenMPToLLVMIRTranslation] Avoid use of ConstantExpr::getSizeOf() (PR #223363)

Nikita Popov llvmlistbot at llvm.org
Mon Sep 14 04:18:29 PDT 2026


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/223363

This will no longer be supported in ptradd representation. Directly produce constants with the correct size instead.

>From 87ad5250330497722715747b4d4d5d17c3b65f82 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 14 Sep 2026 13:15:44 +0200
Subject: [PATCH] [OpenMPToLLVMIRTranslation] Avoid use of
 ConstantExpr::getSizeOf()

This will no longer be supported in ptradd representation. Directly
produce constants with the correct size instead.
---
 .../Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp      | 11 ++++++++---
 mlir/test/Target/LLVMIR/openmp-iterator.mlir          |  6 +++---
 mlir/test/Target/LLVMIR/openmp-llvm.mlir              |  2 +-
 mlir/test/Target/LLVMIR/openmp-task-charbox.mlir      |  2 +-
 .../test/Target/LLVMIR/openmp-task-privatization.mlir |  2 +-
 mlir/test/Target/LLVMIR/openmp-taskloop-cancel.mlir   |  6 +++---
 .../LLVMIR/openmp-taskloop-cancellation-point.mlir    |  4 ++--
 mlir/test/Target/LLVMIR/openmp-taskloop-charbox.mlir  |  4 ++--
 mlir/test/Target/LLVMIR/openmp-taskloop.mlir          |  4 ++--
 mlir/test/Target/LLVMIR/openmp-taskwait-depend.mlir   |  2 +-
 10 files changed, 24 insertions(+), 19 deletions(-)

diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
index a543759a8f282..340b7c2f88f98 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -2856,7 +2856,8 @@ void TaskContextStructManager::generateTaskContextStruct() {
   llvm::DataLayout dataLayout =
       builder.GetInsertBlock()->getModule()->getDataLayout();
   llvm::Type *intPtrTy = builder.getIntPtrTy(dataLayout);
-  llvm::Constant *allocSize = llvm::ConstantExpr::getSizeOf(structTy);
+  llvm::Value *allocSize =
+      builder.CreateTypeSize(intPtrTy, dataLayout.getTypeAllocSize(structTy));
 
   // Heap allocate the structure
   structPtr = builder.CreateMalloc(intPtrTy, allocSize,
@@ -3176,7 +3177,10 @@ buildDependData(OperandRange dependVars, std::optional<ArrayAttr> dependKinds,
 
   // Heap-allocate the kmp_depend_info array so we don't risk
   // dynamic-sized alloca outside the entry block (e.g. inside loops).
-  llvm::Constant *allocSize = llvm::ConstantExpr::getSizeOf(dependInfoTy);
+  llvm::DataLayout dataLayout =
+      builder.GetInsertBlock()->getModule()->getDataLayout();
+  llvm::Value *allocSize = builder.CreateTypeSize(
+      ompBuilder.SizeTy, dataLayout.getTypeAllocSize(dependInfoTy));
   llvm::Value *depArray =
       builder.CreateMalloc(ompBuilder.SizeTy, allocSize, totalCount,
                            /*MallocF=*/nullptr, ".dep.arr.addr");
@@ -8074,9 +8078,10 @@ static void mapParentWithMembers(
         // (e.g. if lowAddr happens to be the first member), which isn't
         // correct, even if the runtimes is sometimes fine with it so, in these
         // scenarios we select the types size instead.
+        llvm::DataLayout dataLayout = builder.GetInsertBlock()->getDataLayout();
         auto sizeSel = builder.CreateSelect(
             builder.CreateICmpNE(builder.getInt64(0), sizeCalc), sizeCalc,
-            isPtrMap ? llvm::ConstantExpr::getSizeOf(builder.getPtrTy())
+            isPtrMap ? builder.getInt64(dataLayout.getPointerSize())
                      : mapData.Sizes[mapDataOverlapIdx]);
         combinedInfo.Sizes.emplace_back(sizeSel);
         lowAddr = builder.CreateConstGEP1_32(
diff --git a/mlir/test/Target/LLVMIR/openmp-iterator.mlir b/mlir/test/Target/LLVMIR/openmp-iterator.mlir
index 2664159cb4481..7fdc6f15b0311 100644
--- a/mlir/test/Target/LLVMIR/openmp-iterator.mlir
+++ b/mlir/test/Target/LLVMIR/openmp-iterator.mlir
@@ -323,7 +323,7 @@ llvm.func @omp_task_depend_iterator_simple(%addr : !llvm.ptr) {
 
 // CHECK-LABEL: define void @omp_task_depend_iterator_simple
 // CHECK-SAME: (ptr %[[ADDR:[0-9]+]])
-// CHECK: %[[DEP_ARR:.*]] = tail call ptr @malloc(i64 %mallocsize)
+// CHECK: %[[DEP_ARR:.*]] = tail call ptr @malloc(i64 200)
 //
 // Iterator loop: preheader -> header -> cond -> body -> inc -> header...
 // CHECK: omp_dep_iterator.header:
@@ -369,7 +369,7 @@ llvm.func @omp_task_depend_iterator_mixed(%addr : !llvm.ptr, %plain : !llvm.ptr)
 
 // CHECK-LABEL: define void @omp_task_depend_iterator_mixed
 // CHECK-SAME: (ptr %[[ADDR2:[0-9]+]], ptr %[[PLAIN:[0-9]+]])
-// CHECK: %[[DEP_ARR2:.*]] = tail call ptr @malloc(i64 %mallocsize)
+// CHECK: %[[DEP_ARR2:.*]] = tail call ptr @malloc(i64 220)
 //
 // Plain entry at index 0
 // CHECK: %[[PLAIN_ENTRY:.*]] = getelementptr inbounds %struct.kmp_dep_info, ptr %[[DEP_ARR2]], i64 0
@@ -482,7 +482,7 @@ module attributes {omp.is_target_device = false, omp.target_triples = ["amdgcn-a
 
 // TARGET-LABEL: define void @omp_target_depend_iterator
 // TARGET-SAME: (ptr %[[ADDR:[0-9]+]])
-// TARGET-DAG: %[[DEP_ARR:.*]] = tail call ptr @malloc(i64 %mallocsize)
+// TARGET-DAG: %[[DEP_ARR:.*]] = tail call ptr @malloc(i64 200)
 //
 // Iterator loop: preheader -> header -> cond -> body -> inc -> header...
 // TARGET: omp_dep_iterator.header:
diff --git a/mlir/test/Target/LLVMIR/openmp-llvm.mlir b/mlir/test/Target/LLVMIR/openmp-llvm.mlir
index 61123ef87e3ba..96b60d586baed 100644
--- a/mlir/test/Target/LLVMIR/openmp-llvm.mlir
+++ b/mlir/test/Target/LLVMIR/openmp-llvm.mlir
@@ -3622,7 +3622,7 @@ llvm.func @task(%arg0 : !llvm.ptr) {
 //                ...
 // CHECK:         br label %omp.private.init
 // CHECK:       omp.private.init:
-// CHECK:         %[[TASK_STRUCT:.*]] = tail call ptr @malloc(i64 ptrtoint (ptr getelementptr ({ i32 }, ptr null, i32 1) to i64))
+// CHECK:         %[[TASK_STRUCT:.*]] = tail call ptr @malloc(i64 4)
 // CHECK:         %[[GEP:.*]] = getelementptr { i32 }, ptr %[[TASK_STRUCT:.*]], i32 0, i32 0
 // CHECK:         br label %omp.private.copy1
 // CHECK:       omp.private.copy1:
diff --git a/mlir/test/Target/LLVMIR/openmp-task-charbox.mlir b/mlir/test/Target/LLVMIR/openmp-task-charbox.mlir
index 7a448f74ed648..e84366c94b7f8 100644
--- a/mlir/test/Target/LLVMIR/openmp-task-charbox.mlir
+++ b/mlir/test/Target/LLVMIR/openmp-task-charbox.mlir
@@ -53,7 +53,7 @@ module {
 // CHECK:       entry:                                            ; preds = %[[VAL_3:.*]]
 // CHECK:         br label %[[VAL_4:.*]]
 // CHECK:       omp.private.init:                                 ; preds = %[[VAL_2]]
-// CHECK:         %[[VAL_5:.*]] = tail call ptr @malloc(i64 ptrtoint (ptr getelementptr ({ { ptr, i64, i32, i8, i8, i8, i8 }, { ptr, i64, i32, i8, i8, i8, i8 } }, ptr null, i32 1) to i64))
+// CHECK:         %[[VAL_5:.*]] = tail call ptr @malloc(i64 48)
 // CHECK:         %[[VAL_6:.*]] = getelementptr { { ptr, i64, i32, i8, i8, i8, i8 }, { ptr, i64, i32, i8, i8, i8, i8 } }, ptr %[[VAL_5]], i32 0, i32 0
 // CHECK:         %[[VAL_7:.*]] = getelementptr { { ptr, i64, i32, i8, i8, i8, i8 }, { ptr, i64, i32, i8, i8, i8, i8 } }, ptr %[[VAL_5]], i32 0, i32 1
 // ...
diff --git a/mlir/test/Target/LLVMIR/openmp-task-privatization.mlir b/mlir/test/Target/LLVMIR/openmp-task-privatization.mlir
index f7a8b970f5f80..a4e324695befa 100644
--- a/mlir/test/Target/LLVMIR/openmp-task-privatization.mlir
+++ b/mlir/test/Target/LLVMIR/openmp-task-privatization.mlir
@@ -35,7 +35,7 @@ llvm.func @task_privatization_test() {
 // CHECK:       entry:
 // CHECK:         br label %omp.private.init
 // CHECK:       omp.private.init:
-// CHECK:         %[[VAL_5:.*]] = tail call ptr @malloc(i64 ptrtoint (ptr getelementptr ([[STRUCT_KMP_PRIVATES_T:.*]], ptr null, i32 1) to i64))
+// CHECK:         %[[VAL_5:.*]] = tail call ptr @malloc(i64 4)
 // CHECK:         %[[VAL_7:.*]] = getelementptr { i32 }, ptr %[[VAL_5]], i32 0, i32 0
 // CHECK:         br label %omp.private.copy
 // CHECK:       omp.private.copy:
diff --git a/mlir/test/Target/LLVMIR/openmp-taskloop-cancel.mlir b/mlir/test/Target/LLVMIR/openmp-taskloop-cancel.mlir
index d4c53b6d0c322..a13880512f3f8 100644
--- a/mlir/test/Target/LLVMIR/openmp-taskloop-cancel.mlir
+++ b/mlir/test/Target/LLVMIR/openmp-taskloop-cancel.mlir
@@ -42,7 +42,7 @@ llvm.func @_QPtest(%arg0: !llvm.ptr {fir.bindc_name = "arg", llvm.noalias, llvm.
 // CHECK:       entry:                                            ; preds = %[[VAL_2:.*]]
 // CHECK:         br label %[[VAL_3:.*]]
 // CHECK:       omp.private.init:                                 ; preds = %[[VAL_1]]
-// CHECK:         %[[VAL_4:.*]] = tail call ptr @malloc(i64 ptrtoint (ptr getelementptr ({ i32 }, ptr null, i32 1) to i64))
+// CHECK:         %[[VAL_4:.*]] = tail call ptr @malloc(i64 4)
 // CHECK:         %[[VAL_5:.*]] = getelementptr { i32 }, ptr %[[VAL_4]], i32 0, i32 0
 // CHECK:         call void @_init(ptr %[[VAL_6:.*]], ptr %[[VAL_5]])
 // CHECK:         br label %[[VAL_7:.*]]
@@ -180,7 +180,7 @@ llvm.func @_QPtest2(%arg0: !llvm.ptr {fir.bindc_name = "arg", llvm.noalias, llvm
 // CHECK:       omp.taskgroup.region:                             ; preds = %[[VAL_67]]
 // CHECK:         br label %[[VAL_71:.*]]
 // CHECK:       omp.private.init:                                 ; preds = %[[VAL_70]]
-// CHECK:         %[[VAL_72:.*]] = tail call ptr @malloc(i64 ptrtoint (ptr getelementptr ({ i32 }, ptr null, i32 1) to i64))
+// CHECK:         %[[VAL_72:.*]] = tail call ptr @malloc(i64 4)
 // CHECK:         %[[VAL_73:.*]] = getelementptr { i32 }, ptr %[[VAL_72]], i32 0, i32 0
 // CHECK:         call void @_init(ptr %[[VAL_74:.*]], ptr %[[VAL_73]])
 // CHECK:         br label %[[VAL_75:.*]]
@@ -315,7 +315,7 @@ llvm.func @_QPtest3(%arg0: !llvm.ptr {fir.bindc_name = "arg", llvm.noalias, llvm
 // CHECK:       entry:                                            ; preds = %[[VAL_138:.*]]
 // CHECK:         br label %[[VAL_139:.*]]
 // CHECK:       omp.private.init:                                 ; preds = %[[VAL_137]]
-// CHECK:         %[[VAL_140:.*]] = tail call ptr @malloc(i64 ptrtoint (ptr getelementptr ({ i32 }, ptr null, i32 1) to i64))
+// CHECK:         %[[VAL_140:.*]] = tail call ptr @malloc(i64 4)
 // CHECK:         %[[VAL_141:.*]] = getelementptr { i32 }, ptr %[[VAL_140]], i32 0, i32 0
 // CHECK:         call void @_init(ptr %[[VAL_142:.*]], ptr %[[VAL_141]])
 // CHECK:         br label %[[VAL_143:.*]]
diff --git a/mlir/test/Target/LLVMIR/openmp-taskloop-cancellation-point.mlir b/mlir/test/Target/LLVMIR/openmp-taskloop-cancellation-point.mlir
index 19e1674ba788e..c08b66d561891 100644
--- a/mlir/test/Target/LLVMIR/openmp-taskloop-cancellation-point.mlir
+++ b/mlir/test/Target/LLVMIR/openmp-taskloop-cancellation-point.mlir
@@ -42,7 +42,7 @@ llvm.func @_QPtest(%arg0: !llvm.ptr {fir.bindc_name = "arg", llvm.noalias, llvm.
 // CHECK:       entry:                                            ; preds = %[[VAL_2:.*]]
 // CHECK:         br label %[[VAL_3:.*]]
 // CHECK:       omp.private.init:                                 ; preds = %[[VAL_1]]
-// CHECK:         %[[VAL_4:.*]] = tail call ptr @malloc(i64 ptrtoint (ptr getelementptr ({ i32 }, ptr null, i32 1) to i64))
+// CHECK:         %[[VAL_4:.*]] = tail call ptr @malloc(i64 4)
 // CHECK:         %[[VAL_5:.*]] = getelementptr { i32 }, ptr %[[VAL_4]], i32 0, i32 0
 // CHECK:         call void @_init(ptr %[[VAL_6:.*]], ptr %[[VAL_5]])
 // CHECK:         br label %[[VAL_7:.*]]
@@ -180,7 +180,7 @@ llvm.func @_QPtest2(%arg0: !llvm.ptr {fir.bindc_name = "arg", llvm.noalias, llvm
 // CHECK:       omp.taskgroup.region:                             ; preds = %[[VAL_67]]
 // CHECK:         br label %[[VAL_71:.*]]
 // CHECK:       omp.private.init:                                 ; preds = %[[VAL_70]]
-// CHECK:         %[[VAL_72:.*]] = tail call ptr @malloc(i64 ptrtoint (ptr getelementptr ({ i32 }, ptr null, i32 1) to i64))
+// CHECK:         %[[VAL_72:.*]] = tail call ptr @malloc(i64 4)
 // CHECK:         %[[VAL_73:.*]] = getelementptr { i32 }, ptr %[[VAL_72]], i32 0, i32 0
 // CHECK:         call void @_init(ptr %[[VAL_74:.*]], ptr %[[VAL_73]])
 // CHECK:         br label %[[VAL_75:.*]]
diff --git a/mlir/test/Target/LLVMIR/openmp-taskloop-charbox.mlir b/mlir/test/Target/LLVMIR/openmp-taskloop-charbox.mlir
index 9c051092ebcee..2c5ed2ce1c33d 100644
--- a/mlir/test/Target/LLVMIR/openmp-taskloop-charbox.mlir
+++ b/mlir/test/Target/LLVMIR/openmp-taskloop-charbox.mlir
@@ -59,7 +59,7 @@ module {
 // CHECK-NEXT:   br label %omp.private.init
 // CHECK-EMPTY:
 // CHECK-NEXT: omp.private.init:{{.*}}; preds = %entry
-// CHECK-NEXT:   %[[TASK_CONTEXT:.*]] = tail call ptr @malloc(i64 ptrtoint (ptr getelementptr ({ { ptr, i64 } }, ptr null, i32 1) to i64))
+// CHECK-NEXT:   %[[TASK_CONTEXT:.*]] = tail call ptr @malloc(i64 16)
 // CHECK-NEXT:   %[[TASK_CONTEXT_BOX:.*]] = getelementptr { { ptr, i64 } }, ptr %[[TASK_CONTEXT]], i32 0, i32 0
 // CHECK-NEXT:   %[[INIT_PTR:.*]] = extractvalue { ptr, i64 } %[[ORIG_BOX]], 0
 // CHECK-NEXT:   %[[INIT_LEN:.*]] = extractvalue { ptr, i64 } %[[ORIG_BOX]], 1
@@ -194,7 +194,7 @@ module {
 // CHECK-NEXT:   %[[SRC_TASK_DATA:.*]] = getelementptr { %struct.kmp_task_ompbuilder_t, { i64, i64, i64, ptr } }, ptr %[[SRC_TASK]], i32 0, i32 1
 // CHECK-NEXT:   %[[SRC_CONTEXT_ADDR:.*]] = getelementptr { i64, i64, i64, ptr }, ptr %[[SRC_TASK_DATA]], i32 0, i32 3
 // CHECK-NEXT:   %[[SRC_CONTEXT:.*]] = load ptr, ptr %[[SRC_CONTEXT_ADDR]], align 8
-// CHECK-NEXT:   %[[DEST_CONTEXT:.*]] = tail call ptr @malloc(i64 ptrtoint (ptr getelementptr ({ { ptr, i64 } }, ptr null, i32 1) to i64))
+// CHECK-NEXT:   %[[DEST_CONTEXT:.*]] = tail call ptr @malloc(i64 16)
 // CHECK-NEXT:   store ptr %[[DEST_CONTEXT]], ptr %[[DEST_CONTEXT_ADDR]], align 8
 // CHECK-NEXT:   %[[SRC_BOX_ADDR:.*]] = getelementptr { { ptr, i64 } }, ptr %[[SRC_CONTEXT]], i32 0, i32 0
 // CHECK-NEXT:   %[[DEST_BOX_ADDR:.*]] = getelementptr { { ptr, i64 } }, ptr %[[DEST_CONTEXT]], i32 0, i32 0
diff --git a/mlir/test/Target/LLVMIR/openmp-taskloop.mlir b/mlir/test/Target/LLVMIR/openmp-taskloop.mlir
index 30dd881f10467..be83ef88cec97 100644
--- a/mlir/test/Target/LLVMIR/openmp-taskloop.mlir
+++ b/mlir/test/Target/LLVMIR/openmp-taskloop.mlir
@@ -44,7 +44,7 @@ llvm.func @_QPtest() {
 // CHECK:       entry:                                            ; preds = %[[VAL_3:.*]]
 // CHECK:         br label %[[VAL_4:.*]]
 // CHECK:       omp.private.init:                                 ; preds = %[[VAL_2]]
-// CHECK:         %[[VAL_5:.*]] = tail call ptr @malloc(i64 ptrtoint (ptr getelementptr ({ i32 }, ptr null, i32 1) to i64))
+// CHECK:         %[[VAL_5:.*]] = tail call ptr @malloc(i64 4)
 // CHECK:         %[[VAL_6:.*]] = getelementptr { i32 }, ptr %[[VAL_5]], i32 0, i32 0
 // CHECK:         br label %[[VAL_7:.*]]
 // CHECK:       omp.private.copy:                                 ; preds = %[[VAL_4]]
@@ -146,7 +146,7 @@ llvm.func @_QPtest() {
 // CHECK:         %[[VAL_66:.*]] = getelementptr { %[[VAL_63]], { i64, i64, i64, ptr } }, ptr %[[VAL_67:.*]], i32 0, i32 1
 // CHECK:         %[[VAL_68:.*]] = getelementptr { i64, i64, i64, ptr }, ptr %[[VAL_66]], i32 0, i32 3
 // CHECK:         %[[VAL_69:.*]] = load ptr, ptr %[[VAL_68]], align 8
-// CHECK:         %[[VAL_70:.*]] = tail call ptr @malloc(i64 ptrtoint (ptr getelementptr ({ i32 }, ptr null, i32 1) to i64))
+// CHECK:         %[[VAL_70:.*]] = tail call ptr @malloc(i64 4)
 // CHECK:         store ptr %[[VAL_70]], ptr %[[VAL_65]], align 8
 // CHECK:         %[[VAL_71:.*]] = getelementptr { i32 }, ptr %[[VAL_69]], i32 0, i32 0
 // CHECK:         %[[VAL_72:.*]] = getelementptr { i32 }, ptr %[[VAL_70]], i32 0, i32 0
diff --git a/mlir/test/Target/LLVMIR/openmp-taskwait-depend.mlir b/mlir/test/Target/LLVMIR/openmp-taskwait-depend.mlir
index 24a26805df618..fd29efeca3b9f 100644
--- a/mlir/test/Target/LLVMIR/openmp-taskwait-depend.mlir
+++ b/mlir/test/Target/LLVMIR/openmp-taskwait-depend.mlir
@@ -25,7 +25,7 @@ llvm.func @taskwait_depend_iterator(%x: !llvm.ptr) {
 
 // CHECK-LABEL: define void @taskwait_depend_iterator
 // CHECK-SAME: (ptr[[xaddr:.+]])
-// CHECK: %[[dep_arr_addr:.+]] = tail call ptr @malloc(i64 %mallocsize)
+// CHECK: %[[dep_arr_addr:.+]] = tail call ptr @malloc(i64 200)
 //
 // CHECK: omp_dep_iterator.header:
 // CHECK: %[[iv:.*]] = phi i64 [ 0, %omp_dep_iterator.preheader ], [ %[[next:.*]], %omp_dep_iterator.inc ]



More information about the Mlir-commits mailing list