Skip to content

Conversation

@RiverDave
Copy link
Collaborator

@RiverDave RiverDave commented Nov 15, 2025

Bringing the changes we performed at: llvm/llvm-project#161028 down to the incubator isn't as straight-forward as I though. Therefore — this PR might be a bit complicated, bare with me :)

The main purpose of this is to bring the recent upstreamed TargetAddressSpaceAttr and couple it with PointerType and GlobalOp. The main challenge is that this collides with the already implemented infrastructure related to AS that revolves around our unified enum approach. My main rationale here is that we let our new attribute to coexist with the already existing cir::AddressSpaceAttr so that we don't break any tests related to offload-type languages.

Considering the above what I'm essentially doing is:

  1. Letting TargetAddressSpaceAttr handle numeric target AS as it is done upstream.
  2. The current implementation of AddressSpaceAttr(which handles an enum) will handle language/clang specific AS (CUDA, OpenCL, etc..).
  3. PointerType will hold: TargetAddressSpaceAttr, AddressSpaceAttr or null. We enforce this via a custom verifier. (I would've preferred to enforce this rule via an AttrConstraint, but apparently that doesn't support types? — At least I couldn't make it work.)
  4. GlobalOps will enforce the rule mentioned above as well via AttrConstraints. This works since we can apply constraints to Ops.
  5. Adjusted the assembly format for all tests containing any AS attributes to conform with our new format: target_address_space(n)/clang_address_space(x).
  6. For the relevant Ops: Default address spaces will now be represented as an empty mlir::Attribute = {}
  7. Remove any comments that make reference to our old approach regarding AS handling.
  8. Bring any extra upstream changes that deviate with what we have here.

However, not in the scope of this PR but my idea is to improve, revamp the later mentioned AddressSpaceAttr and incorporate most of the feedback we received in: llvm/llvm-project#161028 (comment) — ideally to handle Language AS as I've mentioned. (And yes! I haven't forgotten about the {MemorySpaceAttrInterface} to be coupled with the ptr op).

A couple of extra questions:

  • Would it be suitable to rename AddressSpaceAttr (to something like ClangAddressSpaceAttr or LangAddressSpaceAttr) since after this PR — Our previous attribute will no longer support target AS within that attribute?

@RiverDave RiverDave changed the title [CIR][AddrSpace] Backport TargetAddressSpaceAttr and Support both language(clang) and target address-space attributes in pointer types and Global Ops [CIR] Backport TargetAddressSpaceAttr and Support both existing AS and target AS attributes in pointer types and Global Ops Nov 17, 2025
@github-actions
Copy link

github-actions bot commented Nov 19, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions h,c,cpp,cl -- clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h clang/include/clang/CIR/Dialect/IR/CIRTypes.h clang/lib/CIR/CodeGen/CIRGenBuilder.h clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp clang/lib/CIR/CodeGen/CIRGenCUDANV.cpp clang/lib/CIR/CodeGen/CIRGenDecl.cpp clang/lib/CIR/CodeGen/CIRGenExpr.cpp clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp clang/lib/CIR/CodeGen/CIRGenModule.cpp clang/lib/CIR/CodeGen/CIRGenModule.h clang/lib/CIR/CodeGen/CIRGenTypeCache.h clang/lib/CIR/CodeGen/TargetInfo.cpp clang/lib/CIR/CodeGen/TargetInfo.h clang/lib/CIR/Dialect/IR/CIRAttrs.cpp clang/lib/CIR/Dialect/IR/CIRDialect.cpp clang/lib/CIR/Dialect/IR/CIRTypes.cpp clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp clang/test/CIR/CodeGen/HIP/address-spaces.cpp clang/test/CIR/CodeGen/OpenCL/addrspace-alloca.cl clang/test/CIR/CodeGen/OpenCL/array-decay.cl clang/test/CIR/CodeGen/OpenCL/global.cl clang/test/CIR/CodeGen/OpenCL/printf.cl clang/test/CIR/CodeGen/OpenCL/static-vardecl.cl clang/test/CIR/CodeGen/OpenCL/str_literals.cl clang/test/CIR/CodeGen/address-space-conversion.cpp clang/test/CIR/CodeGen/address-space.c --diff_from_common_commit

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 5e022630f..4935322b5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -1154,7 +1154,8 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
       return entry;
   }
 
-  mlir::Attribute declCIRAS = cir::toCIRAddressSpaceAttr(&getMLIRContext(), getGlobalVarAddressSpace(d));
+  mlir::Attribute declCIRAS = cir::toCIRAddressSpaceAttr(
+      &getMLIRContext(), getGlobalVarAddressSpace(d));
   // TODO(cir): do we need to strip pointer casts for Entry?
 
   auto loc = getLoc(d->getSourceRange());
@@ -1977,7 +1978,8 @@ CIRGenModule::getAddrOfGlobalTemporary(const MaterializeTemporaryExpr *expr,
       linkage = cir::GlobalLinkageKind::InternalLinkage;
     }
   }
-  mlir::Attribute targetAS = cir::toCIRAddressSpaceAttr(&getMLIRContext(), addrSpace);
+  mlir::Attribute targetAS =
+      cir::toCIRAddressSpaceAttr(&getMLIRContext(), addrSpace);
 
   auto loc = getLoc(expr->getSourceRange());
   auto gv = createGlobalOp(*this, loc, name, type, isConstant, targetAS,
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 4256b89a2..eb8962d23 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -55,9 +55,9 @@ using namespace mlir;
 #include "clang/CIR/Dialect/IR/CIROpsDialect.cpp.inc"
 #include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
 #include "clang/CIR/Interfaces/CIROpInterfaces.h"
-#include <clang/CIR/MissingFeatures.h>
-#include <clang/CIR/Dialect/IR/CIRTypes.h>
 #include <clang/CIR/Dialect/IR/CIRDialect.h>
+#include <clang/CIR/Dialect/IR/CIRTypes.h>
+#include <clang/CIR/MissingFeatures.h>
 
 //===----------------------------------------------------------------------===//
 // CIR Dialect
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 080c6d9ba..7d3703afb 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -379,8 +379,6 @@ void lowerAnnotationValue(
   }
 }
 
-
-
 // Get addrspace by converting a pointer type.
 // TODO: The approach here is a little hacky. We should access the target info
 // directly to convert the address space of global op, similar to what we do
@@ -5030,9 +5028,9 @@ void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
   converter.addConversion([&,
                            lowerModule](cir::PointerType type) -> mlir::Type {
     mlir::Attribute addrSpaceAttr = type.getAddrSpace();
-    unsigned addrSpace = addrSpaceAttr
-        ? getTargetAddrSpaceFromASAttr(addrSpaceAttr, lowerModule)
-        : 0; // Default address space
+    unsigned addrSpace =
+        addrSpaceAttr ? getTargetAddrSpaceFromASAttr(addrSpaceAttr, lowerModule)
+                      : 0; // Default address space
     return mlir::LLVM::LLVMPointerType::get(type.getContext(), addrSpace);
   });
   converter.addConversion([&](cir::VPtrType type) -> mlir::Type {

@RiverDave RiverDave changed the title [CIR] Backport TargetAddressSpaceAttr and Support both existing AS and target AS attributes in pointer types and Global Ops [CIR] Backport TargetAddressSpaceAttr and Support both existing Lang AS and target AS attributes in pointer types and Global Ops Nov 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants