https://github.com/xakep8 created 
https://github.com/llvm/llvm-project/pull/218114

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.

Fixes #217687 

>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] [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) 
{

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to