https://github.com/xakep8 updated https://github.com/llvm/llvm-project/pull/218114
>From 58c77126e53744eea5596370f1ae795501528930 Mon Sep 17 00:00:00 2001 From: Kunal Dubey <[email protected]> Date: Fri, 21 Aug 2026 23:54:44 +0530 Subject: [PATCH 1/2] [CIR] Preserve const pointee information on function params Attached a CIR parameter attribute for function paramerters whose source type has a const-qualier. This preserves source signature info in declarations without changing the lowered pointer type itself. This keeps the ordinary pointer/lvalue typing unchanged while making the qualifier available on the signature side. --- .../include/clang/CIR/Dialect/IR/CIRDialect.td | 1 + clang/lib/CIR/CodeGen/CIRGenCall.cpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td index d974cb1fa4544..57ce67805fdad 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.td @@ -90,6 +90,7 @@ def CIR_Dialect : Dialect { static llvm::StringRef getAMDGPUSramEccAttrName() { return "cir.amdgpu_sramecc"; } static llvm::StringRef getOpenCLKernelArgMetadataAttrName() { return "cir.cl.kernel_arg_metadata"; } static llvm::StringRef getDefaultTlsModelAttrName() { return "cir.default_tls_model"; } + static llvm::StringRef getConstPointeeAttrName() { return "cir.const_pointee"; } void registerAttributes(); void registerTypes(); diff --git a/clang/lib/CIR/CodeGen/CIRGenCall.cpp b/clang/lib/CIR/CodeGen/CIRGenCall.cpp index 3e689e031f7ad..5602d163bdd62 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCall.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCall.cpp @@ -17,7 +17,9 @@ #include "CIRGenFunctionInfo.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/IR/Attributes.h" +#include "clang/AST/TypeBase.h" #include "clang/CIR/ABIArgInfo.h" +#include "clang/CIR/Dialect/IR/CIRDialect.h" #include "clang/CIR/MissingFeatures.h" #include "llvm/ADT/FloatingPointMode.h" #include "llvm/ADT/StringSet.h" @@ -663,6 +665,16 @@ void CIRGenModule::constructFunctionReturnAttributes( } } +static bool hasConstQualifiedPointee(QualType type) { + if (const auto *ptrTy = type->getAs<PointerType>()) + return ptrTy->getPointeeType().isConstQualified(); + + if (const auto *refTy = type->getAs<ReferenceType>()) + return refTy->getPointeeType().isConstQualified(); + + return false; +} + void CIRGenModule::constructFunctionArgumentAttributes( const CIRGenFunctionInfo &info, const Decl *targetDecl, bool isThunk, bool attrOnCallSite, llvm::MutableArrayRef<mlir::NamedAttrList> argAttrs) { @@ -771,6 +783,11 @@ void CIRGenModule::constructFunctionArgumentAttributes( argAttrList.set(mlir::LLVM::LLVMDialect::getNoAliasAttrName(), mlir::UnitAttr::get(&getMLIRContext())); + // const pointer handling + if (pvd && hasConstQualifiedPointee(pvd->getType())) + argAttrList.set(cir::CIRDialect::getConstPointeeAttrName(), + mlir::UnitAttr::get(&getMLIRContext())); + // __attribute__((nonnull)) on pointer parameters. Checks both // per-parameter and function-level nonnull attributes. if (pvd && argType->isAnyPointerType() && !codeGenOpts.NullPointerIsValid) { >From 1d393989b748c5d3105f29c091594d44c980cd80 Mon Sep 17 00:00:00 2001 From: Kunal Dubey <[email protected]> Date: Sat, 22 Aug 2026 14:22:09 +0530 Subject: [PATCH 2/2] [CIR] Updated test fallout from the const pointee argAttr addition --- clang/test/CIR/CodeGen/builtin-cpu-supports.c | 4 ++-- clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp | 4 ++-- clang/test/CIR/CodeGen/restrict-noalias.c | 4 ++-- clang/test/CIR/CodeGenBuiltins/builtin-call.cpp | 2 +- clang/test/CIR/CodeGenBuiltins/builtin-printf.cpp | 6 +++--- clang/test/CIR/Transforms/idiom-recognizer.cpp | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/clang/test/CIR/CodeGen/builtin-cpu-supports.c b/clang/test/CIR/CodeGen/builtin-cpu-supports.c index eecde11ce26f3..64d480c6f6a51 100644 --- a/clang/test/CIR/CodeGen/builtin-cpu-supports.c +++ b/clang/test/CIR/CodeGen/builtin-cpu-supports.c @@ -23,7 +23,7 @@ extern void a(const char *); // CIR-NEXT: cir.if %[[RES]] { // CIR-NEXT: %[[STR:.*]] = cir.get_global // CIR-NEXT: %[[STR_DECAY:.*]] = cir.cast array_to_ptrdecay %[[STR]] : !cir.ptr<!cir.array<!s8i x 7>> -> !cir.ptr<!s8i> -// CIR-NEXT: cir.call @a(%[[STR_DECAY]]) : (!cir.ptr<!s8i> {llvm.noundef}) -> () +// CIR-NEXT: cir.call @a(%[[STR_DECAY]]) : (!cir.ptr<!s8i> {cir.const_pointee, llvm.noundef}) -> () // CIR-NEXT: } // CIR-NEXT: } // CIR-NEXT: cir.scope { @@ -37,7 +37,7 @@ extern void a(const char *); // CIR-NEXT: cir.if %[[RES]] { // CIR-NEXT: %[[STR:.*]] = cir.get_global // CIR-NEXT: %[[STR_DECAY:.*]] = cir.cast array_to_ptrdecay %[[STR]] : !cir.ptr<!cir.array<!s8i x 5>> -> !cir.ptr<!s8i> -// CIR-NEXT: cir.call @a(%[[STR_DECAY]]) : (!cir.ptr<!s8i> {llvm.noundef}) -> () +// CIR-NEXT: cir.call @a(%[[STR_DECAY]]) : (!cir.ptr<!s8i> {cir.const_pointee, llvm.noundef}) -> () // CIR-NEXT: } // CIR-NEXT: } diff --git a/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp b/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp index b24b423d809bd..458b024664533 100644 --- a/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp +++ b/clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp @@ -58,7 +58,7 @@ void cxx_rewritten_binary_operator_complex_expr() { // CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} : !cir.ptr<!rec_ComplexItem> // CIR: %[[R_ADDR:.*]] = cir.alloca "r" {{.*}} init : !cir.ptr<!cir.complex<!s32i>> // CIR: %[[TMP_ADDR:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<!rec_SpaceshipComplexResult> -// CIR: %[[OP_RESULT:.*]] = cir.call @_ZNK11ComplexItemssERKS_(%[[A_ADDR]], %[[B_ADDR]]) : (!cir.ptr<!rec_ComplexItem> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}, !cir.ptr<!rec_ComplexItem> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}) -> !rec_SpaceshipComplexResult +// CIR: %[[OP_RESULT:.*]] = cir.call @_ZNK11ComplexItemssERKS_(%[[A_ADDR]], %[[B_ADDR]]) : (!cir.ptr<!rec_ComplexItem> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}, !cir.ptr<!rec_ComplexItem> {cir.const_pointee, llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}) -> !rec_SpaceshipComplexResult // CIR: cir.store {{.*}} %[[OP_RESULT]], %[[TMP_ADDR]] : !rec_SpaceshipComplexResult, !cir.ptr<!rec_SpaceshipComplexResult> // CIR: %[[CONST_0:.*]] = cir.const #cir.int<0> : !s32i // CIR: %[[RESULT:.*]] = cir.call @_ZNK22SpaceshipComplexResultltEi(%[[TMP_ADDR]], %[[CONST_0]]) : (!cir.ptr<!rec_SpaceshipComplexResult> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}, !s32i {llvm.noundef}) -> (!cir.complex<!s32i> {llvm.noundef}) @@ -114,7 +114,7 @@ void cxx_rewritten_binary_operator_aggr_expr() { // CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} : !cir.ptr<!rec_Item> // CIR: %[[R_ADDR:.*]] = cir.alloca "r" {{.*}} init : !cir.ptr<!rec_Result> // CIR: %[[TMP_ADDR:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<!rec_SpaceshipResult> -// CIR: %[[OP_RESULT:.*]] = cir.call @_ZNK4ItemssERKS_(%[[A_ADDR]], %[[B_ADDR]]) : (!cir.ptr<!rec_Item> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}, !cir.ptr<!rec_Item> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}) -> !rec_SpaceshipResult +// CIR: %[[OP_RESULT:.*]] = cir.call @_ZNK4ItemssERKS_(%[[A_ADDR]], %[[B_ADDR]]) : (!cir.ptr<!rec_Item> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}, !cir.ptr<!rec_Item> {cir.const_pointee, llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}) -> !rec_SpaceshipResult // CIR: cir.store {{.*}} %[[OP_RESULT]], %[[TMP_ADDR]] : !rec_SpaceshipResult, !cir.ptr<!rec_SpaceshipResult> // CIR: %[[CONST_0:.*]] = cir.const #cir.int<0> : !s32i // CIR: %[[RESULT:.*]] = cir.call @_ZNK15SpaceshipResultltEi(%[[TMP_ADDR]], %[[CONST_0]]) : (!cir.ptr<!rec_SpaceshipResult> {llvm.align = 1 : i64, llvm.dereferenceable = 1 : i64, llvm.nonnull, llvm.noundef}, !s32i {llvm.noundef}) -> !rec_Result diff --git a/clang/test/CIR/CodeGen/restrict-noalias.c b/clang/test/CIR/CodeGen/restrict-noalias.c index b69de97b07864..da93c912d90bb 100644 --- a/clang/test/CIR/CodeGen/restrict-noalias.c +++ b/clang/test/CIR/CodeGen/restrict-noalias.c @@ -28,8 +28,8 @@ void test_builtin(const char *__restrict fmt) { } // Builtins must NOT get noalias from restrict (matching OGCG behavior). -// CIR: cir.func {{.*}} @test_builtin(%arg0: !cir.ptr<!s8i> {llvm.noalias, llvm.noundef} -// CIR: cir.call @printf(%{{.*}}) : (!cir.ptr<!s8i> {llvm.noundef}) -> !s32i +// CIR: cir.func {{.*}} @test_builtin(%arg0: !cir.ptr<!s8i> {cir.const_pointee, llvm.noalias, llvm.noundef} +// CIR: cir.call @printf(%{{.*}}) : (!cir.ptr<!s8i> {cir.const_pointee, llvm.noundef}) -> !s32i // LLVM: define dso_local void @test_builtin(ptr noalias noundef %{{.*}}) // LLVM: call i32 (ptr, ...) @printf(ptr noundef %{{.*}}) diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-call.cpp b/clang/test/CIR/CodeGenBuiltins/builtin-call.cpp index 51c8db967e5d6..940112d84bf27 100644 --- a/clang/test/CIR/CodeGenBuiltins/builtin-call.cpp +++ b/clang/test/CIR/CodeGenBuiltins/builtin-call.cpp @@ -85,7 +85,7 @@ void library_builtins() { // CIR: cir.func{{.*}} @_Z16library_builtinsv() // CIR: %[[NULL:.+]] = cir.const #cir.ptr<null> : !cir.ptr<!s8i> -// CIR: cir.call @printf(%[[NULL]]) nothrow : (!cir.ptr<!s8i> {llvm.noundef}) -> !s32i +// CIR: cir.call @printf(%[[NULL]]) nothrow : (!cir.ptr<!s8i> {cir.const_pointee, llvm.noundef}) -> !s32i // CIR: cir.call @abort() nothrow {noreturn} : () -> () // LLVM: define{{.*}} void @_Z16library_builtinsv() diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-printf.cpp b/clang/test/CIR/CodeGenBuiltins/builtin-printf.cpp index 5e980f3e9be12..26c26f7d2a42c 100644 --- a/clang/test/CIR/CodeGenBuiltins/builtin-printf.cpp +++ b/clang/test/CIR/CodeGenBuiltins/builtin-printf.cpp @@ -24,16 +24,16 @@ void func(char const * const str, int i) { // CIR: cir.store %[[arg0]], %[[str_ptr]] : !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>> // CIR: cir.store %[[arg1]], %[[i_ptr]] : !s32i, !cir.ptr<!s32i> // CIR: %[[null_ptr:.+]] = cir.const #cir.ptr<null> : !cir.ptr<!s8i> -// CIR: %[[printf_result1:.+]] = cir.call @printf(%[[null_ptr]]) nothrow : (!cir.ptr<!s8i> {llvm.noundef}) -> !s32i +// CIR: %[[printf_result1:.+]] = cir.call @printf(%[[null_ptr]]) nothrow : (!cir.ptr<!s8i> {cir.const_pointee, llvm.noundef}) -> !s32i // CIR: %[[str_fmt_global:.+]] = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 3>> // CIR: %[[str_fmt_ptr:.+]] = cir.cast array_to_ptrdecay %[[str_fmt_global]] : !cir.ptr<!cir.array<!s8i x 3>> -> !cir.ptr<!s8i> // CIR: %[[str_val:.+]] = cir.load{{.*}} %[[str_ptr]] : !cir.ptr<!cir.ptr<!s8i>>, !cir.ptr<!s8i> -// CIR: %[[printf_result2:.+]] = cir.call @printf(%[[str_fmt_ptr]], %[[str_val]]) nothrow : (!cir.ptr<!s8i> {llvm.noundef}, !cir.ptr<!s8i> {llvm.noundef}) -> !s32i +// CIR: %[[printf_result2:.+]] = cir.call @printf(%[[str_fmt_ptr]], %[[str_val]]) nothrow : (!cir.ptr<!s8i> {cir.const_pointee, llvm.noundef}, !cir.ptr<!s8i> {llvm.noundef}) -> !s32i // CIR: %[[full_fmt_global:.+]] = cir.get_global @".str.1" : !cir.ptr<!cir.array<!s8i x 7>> // CIR: %[[full_fmt_ptr:.+]] = cir.cast array_to_ptrdecay %[[full_fmt_global]] : !cir.ptr<!cir.array<!s8i x 7>> -> !cir.ptr<!s8i> // CIR: %[[str_val2:.+]] = cir.load{{.*}} %[[str_ptr]] : !cir.ptr<!cir.ptr<!s8i>>, !cir.ptr<!s8i> // CIR: %[[i_val:.+]] = cir.load{{.*}} %[[i_ptr]] : !cir.ptr<!s32i>, !s32i -// CIR: %[[printf_result3:.+]] = cir.call @printf(%[[full_fmt_ptr]], %[[str_val2]], %[[i_val]]) nothrow : (!cir.ptr<!s8i> {llvm.noundef}, !cir.ptr<!s8i> {llvm.noundef}, !s32i {llvm.noundef}) -> !s32i +// CIR: %[[printf_result3:.+]] = cir.call @printf(%[[full_fmt_ptr]], %[[str_val2]], %[[i_val]]) nothrow : (!cir.ptr<!s8i> {cir.const_pointee, llvm.noundef}, !cir.ptr<!s8i> {llvm.noundef}, !s32i {llvm.noundef}) -> !s32i // CIR: cir.return // CIR: cir.func{{.*}} @printf(!cir.ptr<!s8i> {{.*}}, ...) -> !s32i diff --git a/clang/test/CIR/Transforms/idiom-recognizer.cpp b/clang/test/CIR/Transforms/idiom-recognizer.cpp index edb02677f0947..0206876412b8b 100644 --- a/clang/test/CIR/Transforms/idiom-recognizer.cpp +++ b/clang/test/CIR/Transforms/idiom-recognizer.cpp @@ -55,7 +55,7 @@ unsigned long test_strlen(const char *s) { return strlen(s); } // FINAL: %[[S_ADDR:.*]] = cir.alloca "s" // FINAL: %[[S:.*]] = cir.load{{.*}} %[[S_ADDR]] : // FINAL: cir.call @strlen(%[[S]]) nothrow -// FINAL-SAME: (!cir.ptr<!s8i> {llvm.noundef}) +// FINAL-SAME: (!cir.ptr<!s8i> {cir.const_pointee, llvm.noundef}) // FINAL-SAME: -> !u64i // NO-BUILTIN-MEMCPY: cir.call @strlen // NO-BUILTIN-MEMCPY-SAME: nobuiltins = ["memcpy"] _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
