Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,29 +155,25 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
return cir::PointerType::get(ty);
}

cir::PointerType getPointerTo(mlir::Type ty, cir::TargetAddressSpaceAttr as) {
cir::PointerType getPointerTo(mlir::Type ty,
mlir::ptr::MemorySpaceAttrInterface as) {
return cir::PointerType::get(ty, as);
}

cir::PointerType getPointerTo(mlir::Type ty, clang::LangAS langAS) {
if (langAS == clang::LangAS::Default) // Default address space.
if (langAS == clang::LangAS::Default)
return getPointerTo(ty);

if (clang::isTargetAddressSpace(langAS)) {
unsigned addrSpace = clang::toTargetAddressSpace(langAS);
auto asAttr = cir::TargetAddressSpaceAttr::get(
getContext(), getUI32IntegerAttr(addrSpace));
return getPointerTo(ty, asAttr);
}

llvm_unreachable("language-specific address spaces NYI");
mlir::ptr::MemorySpaceAttrInterface addrSpaceAttr =
cir::toCIRAddressSpaceAttr(*getContext(), langAS);
return getPointerTo(ty, addrSpaceAttr);
}

cir::PointerType getVoidPtrTy(clang::LangAS langAS = clang::LangAS::Default) {
return getPointerTo(cir::VoidType::get(getContext()), langAS);
}

cir::PointerType getVoidPtrTy(cir::TargetAddressSpaceAttr as) {
cir::PointerType getVoidPtrTy(mlir::ptr::MemorySpaceAttrInterface as) {
return getPointerTo(cir::VoidType::get(getContext()), as);
}

Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef CLANG_CIR_DIALECT_IR_CIRATTRS_H
#define CLANG_CIR_DIALECT_IR_CIRATTRS_H

#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributeInterfaces.h"
#include "clang/Basic/AddressSpaces.h"
Expand Down
55 changes: 52 additions & 3 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define CLANG_CIR_DIALECT_IR_CIRATTRS_TD

include "mlir/IR/BuiltinAttributeInterfaces.td"
include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.td"

include "clang/CIR/Dialect/IR/CIRAttrConstraints.td"
include "clang/CIR/Dialect/IR/CIRDialect.td"
Expand Down Expand Up @@ -783,16 +784,64 @@ def CIR_DynamicCastInfoAttr : CIR_Attr<"DynamicCastInfo", "dyn_cast_info"> {
}];
}

//===----------------------------------------------------------------------===//
// LangAddressSpaceAttr
//===----------------------------------------------------------------------===//

def CIR_LangAddressSpaceAttr : CIR_EnumAttr<CIR_LangAddressSpace,
"lang_address_space", [
DeclareAttrInterfaceMethods<MemorySpaceAttrInterface>
]> {
let summary = "Represents a language address space";
let description = [{
Encodes the semantic address spaces defined by the front-end language
(e.g. `__shared__`, `__constant__`, `__local__`). Values are stored using the
`cir::LangAddressSpace` enum, keeping the representation compact and
preserving the qualifier until it is mapped onto target/LLVM address-space
numbers.

Example:
```mlir
!cir.ptr<!s32i, lang_address_space(offload_local)>
cir.global constant external lang_address_space(offload_constant)
```
}];

let builders = [
AttrBuilder<(ins "clang::LangAS":$langAS), [{
return $_get($_ctxt, cir::toCIRLangAddressSpace(langAS));
}]>
];

let assemblyFormat = [{
`(` custom<AddressSpaceValue>($value) `)`
}];

let defaultValue = "cir::LangAddressSpace::Default";

let extraClassDeclaration = [{
unsigned getAsUnsignedValue() const;
}];

let extraClassDefinition = [{
unsigned $cppClass::getAsUnsignedValue() const {
return static_cast<unsigned>(getValue());
}
}];
}

//===----------------------------------------------------------------------===//
// TargetAddressSpaceAttr
//===----------------------------------------------------------------------===//

def CIR_TargetAddressSpaceAttr : CIR_Attr< "TargetAddressSpace",
"target_address_space"> {
"target_address_space", [
DeclareAttrInterfaceMethods<MemorySpaceAttrInterface>
]> {
let summary = "Represents a target-specific numeric address space";
let description = [{
The TargetAddressSpaceAttr represents a target-specific numeric address space,
corresponding to the LLVM IR `addressspace` qualifier and the clang
corresponding to the LLVM IR `addrspace` qualifier and the clang
`address_space` attribute.

A value of zero represents the default address space. The semantics of non-zero
Expand All @@ -806,7 +855,7 @@ def CIR_TargetAddressSpaceAttr : CIR_Attr< "TargetAddressSpace",
```
}];

let parameters = (ins "mlir::IntegerAttr":$value);
let parameters = (ins "unsigned":$value);
let assemblyFormat = "`<` `target` `<` $value `>` `>`";
}

Expand Down
19 changes: 19 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIREnumAttr.td
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define CLANG_CIR_DIALECT_IR_CIRENUMATTR_TD

include "mlir/IR/EnumAttr.td"
include "clang/CIR/Dialect/IR/CIRDialect.td"

class CIR_I32EnumAttr<string name, string summary, list<I32EnumAttrCase> cases>
: I32EnumAttr<name, summary, cases> {
Expand All @@ -35,4 +36,22 @@ class CIR_DefaultValuedEnumParameter<EnumAttrInfo info, string value = "">
let defaultValue = value;
}

def CIR_LangAddressSpace : CIR_I32EnumAttr<
"LangAddressSpace", "language address space kind", [
I32EnumAttrCase<"Default", 0, "default">,
I32EnumAttrCase<"OffloadPrivate", 1, "offload_private">,
I32EnumAttrCase<"OffloadLocal", 2, "offload_local">,
I32EnumAttrCase<"OffloadGlobal", 3, "offload_global">,
I32EnumAttrCase<"OffloadConstant", 4, "offload_constant">,
I32EnumAttrCase<"OffloadGeneric", 5, "offload_generic">
]> {
let description = [{
Enumerates language-specific address spaces used by CIR. These represent
semantic qualifiers from source languages (e.g., CUDA `__shared__`,
OpenCL `__local`) before target lowering.
}];

let genSpecializedAttr = 0;
}

#endif // CLANG_CIR_DIALECT_IR_CIRENUMATTR_TD
20 changes: 17 additions & 3 deletions clang/include/clang/CIR/Dialect/IR/CIRTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef CLANG_CIR_DIALECT_IR_CIRTYPES_H
#define CLANG_CIR_DIALECT_IR_CIRTYPES_H

#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/MLIRContext.h"
Expand Down Expand Up @@ -43,12 +44,25 @@ bool isSized(mlir::Type ty);
//===----------------------------------------------------------------------===//
// AddressSpace helpers
//===----------------------------------------------------------------------===//
cir::TargetAddressSpaceAttr toCIRTargetAddressSpace(mlir::MLIRContext &context,
clang::LangAS langAS);

bool isMatchingAddressSpace(cir::TargetAddressSpaceAttr cirAS,
cir::LangAddressSpace toCIRLangAddressSpace(clang::LangAS langAS);

// Compare a CIR memory space attribute with a Clang LangAS.
bool isMatchingAddressSpace(mlir::ptr::MemorySpaceAttrInterface cirAS,
clang::LangAS as);

/// Convert an AST LangAS to the appropriate CIR address space attribute
/// interface.
mlir::ptr::MemorySpaceAttrInterface
toCIRAddressSpaceAttr(mlir::MLIRContext &ctx, clang::LangAS langAS);

/// Normalize LangAddressSpace::Default to null (empty attribute).
mlir::ptr::MemorySpaceAttrInterface
skipDefaultAddressSpace(mlir::ptr::MemorySpaceAttrInterface addrSpace);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
skipDefaultAddressSpace(mlir::ptr::MemorySpaceAttrInterface addrSpace);
normalizeAddressSpace(mlir::ptr::MemorySpaceAttrInterface addrSpace);


bool isSupportedCIRMemorySpaceAttr(
mlir::ptr::MemorySpaceAttrInterface memorySpace);

} // namespace cir

//===----------------------------------------------------------------------===//
Expand Down
14 changes: 10 additions & 4 deletions clang/include/clang/CIR/Dialect/IR/CIRTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -252,27 +252,31 @@ def CIR_PointerType : CIR_Type<"Pointer", "ptr", [
let parameters = (ins
"mlir::Type":$pointee,
OptionalParameter<
"cir::TargetAddressSpaceAttr">:$addrSpace
"mlir::ptr::MemorySpaceAttrInterface">:$addrSpace
);

let skipDefaultBuilders = 1;
let builders = [
TypeBuilderWithInferredContext<(ins
"mlir::Type":$pointee,
CArg<"cir::TargetAddressSpaceAttr", "nullptr">:$addrSpace), [{
CArg<"mlir::ptr::MemorySpaceAttrInterface", "{}">:$addrSpace), [{
// Drop default address space and replace with empty attribute.
addrSpace = cir::skipDefaultAddressSpace(addrSpace);
return $_get(pointee.getContext(), pointee, addrSpace);
}]>,
TypeBuilder<(ins
"mlir::Type":$pointee,
CArg<"cir::TargetAddressSpaceAttr", "nullptr">:$addrSpace), [{
CArg<"mlir::ptr::MemorySpaceAttrInterface", "{}">:$addrSpace), [{
// Drop default address space and replace with empty attribute.
addrSpace = cir::skipDefaultAddressSpace(addrSpace);
return $_get($_ctxt, pointee, addrSpace);
}]>
];

let assemblyFormat = [{
`<`
$pointee
( `,` ` ` custom<TargetAddressSpace>($addrSpace)^ )?
( `,` ` ` custom<AddressSpaceValue>($addrSpace)^ )?
`>`
}];

Expand Down Expand Up @@ -303,6 +307,8 @@ def CIR_PointerType : CIR_Type<"Pointer", "ptr", [
return false;
}
}];

let genVerifyDecl = 1;
}

//===----------------------------------------------------------------------===//
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/CIR/CodeGen/Address.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef CLANG_LIB_CIR_ADDRESS_H
#define CLANG_LIB_CIR_ADDRESS_H

#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
#include "mlir/IR/Value.h"
#include "clang/AST/CharUnits.h"
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
Expand Down Expand Up @@ -127,7 +128,7 @@ class Address {
return elementType;
}

cir::TargetAddressSpaceAttr getAddressSpace() const {
mlir::ptr::MemorySpaceAttrInterface getAddressSpace() const {
auto ptrTy = mlir::dyn_cast<cir::PointerType>(getType());
return ptrTy.getAddrSpace();
}
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ static RValue emitBuiltinAlloca(CIRGenFunction &cgf, const CallExpr *e,
cgf.getCIRAllocaAddressSpace(),
e->getType()->getPointeeType().getAddressSpace())) {
cgf.cgm.errorNYI(e->getSourceRange(),
"Non-default address space for alloca");
"Address Space Cast for builtin alloca");
}

// Bitcast the alloca to the expected type.
Expand Down Expand Up @@ -825,7 +825,7 @@ decodeFixedType(CIRGenFunction &cgf,
case IITDescriptor::Pointer: {
mlir::Builder builder(context);
auto addrSpace = cir::TargetAddressSpaceAttr::get(
context, builder.getUI32IntegerAttr(descriptor.Pointer_AddressSpace));
context, descriptor.Pointer_AddressSpace);
return cir::PointerType::get(cir::VoidType::get(context), addrSpace);
}
default:
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "CIRGenFunction.h"
#include "CIRGenModule.h"
#include "CIRGenValue.h"
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Value.h"
#include "clang/AST/Attr.h"
Expand Down Expand Up @@ -1455,9 +1456,9 @@ LValue CIRGenFunction::emitCastLValue(const CastExpr *e) {
QualType destTy = getContext().getPointerType(e->getType());

clang::LangAS srcLangAS = e->getSubExpr()->getType().getAddressSpace();
cir::TargetAddressSpaceAttr srcAS;
mlir::ptr::MemorySpaceAttrInterface srcAS;
if (clang::isTargetAddressSpace(srcLangAS))
srcAS = cir::toCIRTargetAddressSpace(getMLIRContext(), srcLangAS);
srcAS = cir::toCIRAddressSpaceAttr(getMLIRContext(), srcLangAS);
else
cgm.errorNYI(
e->getSourceRange(),
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/MissingFeatures.h"

#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
#include "mlir/IR/Location.h"
#include "mlir/IR/Value.h"

Expand Down
5 changes: 3 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenTypeCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_CLANG_LIB_CIR_CIRGENTYPECACHE_H
#define LLVM_CLANG_LIB_CIR_CIRGENTYPECACHE_H

#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
#include "clang/AST/CharUnits.h"
#include "clang/Basic/AddressSpaces.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
Expand Down Expand Up @@ -80,7 +81,7 @@ struct CIRGenTypeCache {
unsigned char SizeAlignInBytes;
};

cir::TargetAddressSpaceAttr cirAllocaAddressSpace;
mlir::ptr::MemorySpaceAttrInterface cirAllocaAddressSpace;

clang::CharUnits getSizeSize() const {
return clang::CharUnits::fromQuantity(SizeSizeInBytes);
Expand All @@ -93,7 +94,7 @@ struct CIRGenTypeCache {
return clang::CharUnits::fromQuantity(PointerAlignInBytes);
}

cir::TargetAddressSpaceAttr getCIRAllocaAddressSpace() const {
mlir::ptr::MemorySpaceAttrInterface getCIRAllocaAddressSpace() const {
return cirAllocaAddressSpace;
}
};
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CIR/CodeGen/TargetInfo.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "TargetInfo.h"
#include "ABIInfo.h"
#include "CIRGenFunction.h"
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"

Expand Down
7 changes: 5 additions & 2 deletions clang/lib/CIR/CodeGen/TargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

#include "ABIInfo.h"
#include "CIRGenTypes.h"
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
#include "clang/Basic/AddressSpaces.h"
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
#include "clang/CIR/Dialect/IR/CIROpsEnums.h"

#include <memory>
#include <utility>
Expand Down Expand Up @@ -48,8 +50,9 @@ class TargetCIRGenInfo {
const ABIInfo &getABIInfo() const { return *info; }

/// Get the address space for alloca.
virtual cir::TargetAddressSpaceAttr getCIRAllocaAddressSpace() const {
return {};
virtual mlir::ptr::MemorySpaceAttrInterface getCIRAllocaAddressSpace() const {
return cir::LangAddressSpaceAttr::get(&info->cgt.getMLIRContext(),
cir::LangAddressSpace::Default);
}

/// Determine whether a call to an unprototyped functions under
Expand Down
Loading