[clang] [llvm] [mlir] [IRBuilder] Refactor for intrinsics const-folding (NFC) (PR #202738)

Nikita Popov via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 10 00:14:04 PDT 2026


================
@@ -2003,17 +2004,18 @@ class IRBuilderBase {
         new AtomicRMWInst(Op, Ptr, Val, *Align, Ordering, SSID, Elementwise));
   }
 
-  CallInst *CreateStructuredGEP(Type *BaseType, Value *PtrBase,
-                                ArrayRef<Value *> Indices,
-                                const Twine &Name = "") {
+  Value *CreateStructuredGEP(Type *BaseType, Value *PtrBase,
+                             ArrayRef<Value *> Indices,
+                             const Twine &Name = "") {
     SmallVector<Value *> Args;
     Args.push_back(PtrBase);
     llvm::append_range(Args, Indices);
 
-    CallInst *Output = CreateIntrinsic(Intrinsic::structured_gep,
-                                       {PtrBase->getType()}, Args, {}, Name);
-    Output->addParamAttr(
-        0, Attribute::get(getContext(), Attribute::ElementType, BaseType));
+    Value *Output = CreateIntrinsic(Intrinsic::structured_gep,
+                                    {PtrBase->getType()}, Args, {}, Name);
+    if (auto *CI = dyn_cast<CallInst>(Output))
+      CI->addParamAttr(
+          0, Attribute::get(getContext(), Attribute::ElementType, BaseType));
----------------
nikic wrote:

This kind of pattern is unsafe: If the intrinsic folds and the result just happens to be a new call, then this will set the attributes on the wrong instruction.

The attribute setting either needs to be directly integrated in CreateIntrinsinc(), or you need something like CreateIntrinsicWithoutFolding that is guaranteed to not fold the intrinsic.

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


More information about the cfe-commits mailing list