Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 5 additions & 8 deletions clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
mlir::TypedAttr getConstPtrAttr(mlir::Type t, int64_t v) {
auto val =
mlir::IntegerAttr::get(mlir::IntegerType::get(t.getContext(), 64), v);
return cir::ConstPtrAttr::get(getContext(), mlir::cast<cir::PointerType>(t),
val);
return cir::ConstPtrAttr::get(t, val);
}

mlir::TypedAttr getConstNullPtrAttr(mlir::Type t) {
Expand Down Expand Up @@ -663,9 +662,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
callOp->setAttr("extra_attrs", extraFnAttr);
} else {
mlir::NamedAttrList empty;
callOp->setAttr("extra_attrs",
cir::ExtraFuncAttributesAttr::get(
getContext(), empty.getDictionary(getContext())));
callOp->setAttr("extra_attrs", cir::ExtraFuncAttributesAttr::get(
empty.getDictionary(getContext())));
}
return callOp;
}
Expand Down Expand Up @@ -719,9 +717,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
tryCallOp->setAttr("extra_attrs", extraFnAttr);
} else {
mlir::NamedAttrList empty;
tryCallOp->setAttr("extra_attrs",
cir::ExtraFuncAttributesAttr::get(
getContext(), empty.getDictionary(getContext())));
tryCallOp->setAttr("extra_attrs", cir::ExtraFuncAttributesAttr::get(
empty.getDictionary(getContext())));
}
return tryCallOp;
}
Expand Down
52 changes: 43 additions & 9 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ def ConstArrayAttr : CIR_Attr<"ConstArray", "const_array", [TypedAttrInterface]>
zeros = typeSize - mlir::cast<mlir::ArrayAttr>(elts).size();

return $_get(type.getContext(), type, elts, zeros);
}]>,
AttrBuilderWithInferredContext<(ins "cir::ArrayType":$type,
"mlir::Attribute":$elts,
"int":$trailingZerosNum), [{
return $_get(type.getContext(), type, elts, trailingZerosNum);
}]>
];

Expand Down Expand Up @@ -510,11 +515,7 @@ def ConstPtrAttr : CIR_Attr<"ConstPtr", "ptr", [TypedAttrInterface]> {
let builders = [
AttrBuilderWithInferredContext<(ins "mlir::Type":$type, "mlir::IntegerAttr":$value), [{
return $_get(type.getContext(), mlir::cast<cir::PointerType>(type), value);
}]>,
AttrBuilder<(ins "mlir::Type":$type,
"mlir::IntegerAttr":$value), [{
return $_get($_ctxt, mlir::cast<cir::PointerType>(type), value);
}]>,
}]>
];
let extraClassDeclaration = [{
bool isNullValue() const { return getValue().getInt() == 0; }
Expand Down Expand Up @@ -614,6 +615,16 @@ def DataMemberAttr : CIR_Attr<"DataMember", "data_member",
```
}];

let builders = [
AttrBuilderWithInferredContext<(ins "cir::DataMemberType":$type), [{
return $_get(type.getContext(), type, std::nullopt);
}]>,
AttrBuilderWithInferredContext<(ins "cir::DataMemberType":$type,
"unsigned":$member_index), [{
return $_get(type.getContext(), type, member_index);
}]>,
];

let genVerifyDecl = 1;

let assemblyFormat = [{
Expand Down Expand Up @@ -733,12 +744,12 @@ def GlobalViewAttr : CIR_Attr<"GlobalView", "global_view", [TypedAttrInterface]>
cir.global external @elt_ptr = #cir.global_view<@rgb, [1]> : !cir.ptr<i8>
cir.global external @table_of_ptrs = #cir.const_array<[#cir.global_view<@rgb, [1]> : !cir.ptr<i8>] : !cir.array<!cir.ptr<i8> x 1>>
```

Note, that unlike LLVM IR's gep instruction, CIR doesn't add the leading zero index
when it's known to be constant zero, e.g. for pointers, i.e. we use indexes exactly
to access sub elements or for the offset. The leading zero index is added later in
the lowering.

Example:
```
struct A {
Expand All @@ -751,7 +762,7 @@ def GlobalViewAttr : CIR_Attr<"GlobalView", "global_view", [TypedAttrInterface]>
```
VTT for B:
```
cir.global linkonce_odr @_ZTT1B = #cir.const_array<[#cir.global_view<@_ZTV1B, [0 : i32, 3 : i32]> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 1>
cir.global linkonce_odr @_ZTT1B = #cir.const_array<[#cir.global_view<@_ZTV1B, [0 : i32, 3 : i32]> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 1>
```
The same for LLVM IR after CIR:
```
Expand Down Expand Up @@ -1189,6 +1200,12 @@ def ExtraFuncAttr : CIR_Attr<"ExtraFuncAttributes", "extra"> {

let parameters = (ins "mlir::DictionaryAttr":$elements);

let builders = [
AttrBuilderWithInferredContext<(ins "mlir::DictionaryAttr":$elements), [{
return $_get(elements.getContext(), elements);
}]>
];

let assemblyFormat = [{ `(` $elements `)` }];

// Printing and parsing also available in CIRDialect.cpp
Expand Down Expand Up @@ -1281,6 +1298,10 @@ class CIR_GlobalCtorDtor<string name, string attrMnemonic,
AttrBuilder<(ins "llvm::StringRef":$name,
CArg<"int", "65536">:$priority), [{
return $_get($_ctxt, mlir::StringAttr::get($_ctxt, name), priority);
}]>,
AttrBuilderWithInferredContext<(ins "mlir::StringAttr":$name,
CArg<"int", "65536">:$priority), [{
return $_get(name.getContext(), name, priority);
}]>
];
let extraClassDeclaration = [{
Expand Down Expand Up @@ -1349,6 +1370,13 @@ def AnnotationAttr : CIR_Attr<"Annotation", "annotation"> {
let parameters = (ins "mlir::StringAttr":$name,
"mlir::ArrayAttr":$args);

let builders = [
AttrBuilderWithInferredContext<(ins "mlir::StringAttr":$name,
CArg<"mlir::ArrayAttr", "{}">:$args), [{
return $_get(name.getContext(), name, args);
}]>
];

let assemblyFormat = "`<` struct($name, $args) `>`";

let extraClassDeclaration = [{
Expand Down Expand Up @@ -1389,6 +1417,12 @@ def GlobalAnnotationValuesAttr : CIR_Attr<"GlobalAnnotationValues",

let parameters = (ins "mlir::ArrayAttr":$annotations);

let builders = [
AttrBuilderWithInferredContext<(ins "mlir::ArrayAttr":$annotations), [{
return $_get(annotations.getContext(), annotations);
}]>
];

let assemblyFormat = [{ $annotations }];

// Enable verifier.
Expand All @@ -1402,7 +1436,7 @@ def GlobalAnnotationValuesAttr : CIR_Attr<"GlobalAnnotationValues",
def AddressPointAttr : CIR_Attr<"AddressPoint", "address_point"> {
let summary = "Address point attribute";

let description = [{
let description = [{
Attribute specifying the address point within a C++ virtual table (vtable).

The `index` (vtable index) parameter identifies which vtable to use within a vtable
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
cir::ArrayType::get(eltTy, finalSize - trailingZerosNum);
auto fullArrayTy = cir::ArrayType::get(eltTy, finalSize);
return cir::ConstArrayAttr::get(
getContext(), fullArrayTy,
fullArrayTy,
mlir::StringAttr::get(str.drop_back(trailingZerosNum),
truncatedArrayTy),
trailingZerosNum);
Expand Down Expand Up @@ -258,11 +258,11 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {

cir::DataMemberAttr getDataMemberAttr(cir::DataMemberType ty,
unsigned memberIndex) {
return cir::DataMemberAttr::get(getContext(), ty, memberIndex);
return cir::DataMemberAttr::get(ty, memberIndex);
}

cir::DataMemberAttr getNullDataMemberAttr(cir::DataMemberType ty) {
return cir::DataMemberAttr::get(getContext(), ty, std::nullopt);
return cir::DataMemberAttr::get(ty);
}

// TODO(cir): Once we have CIR float types, replace this by something like a
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &CallInfo,
}

auto extraFnAttrs = cir::ExtraFuncAttributesAttr::get(
&getMLIRContext(), Attrs.getDictionary(&getMLIRContext()));
Attrs.getDictionary(&getMLIRContext()));

cir::CIRCallOpInterface callLikeOp = emitCallLikeOp(
*this, callLoc, indirectFuncTy, indirectFuncVal, directFuncOp,
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenExprConst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,7 @@ static mlir::TypedAttr emitNullConstant(CIRGenModule &CGM, const RecordDecl *rd,
}

mlir::MLIRContext *mlirContext = record.getContext();
return cir::ConstRecordAttr::get(mlirContext, record,
return cir::ConstRecordAttr::get(record,
mlir::ArrayAttr::get(mlirContext, elements));
}

Expand Down
14 changes: 7 additions & 7 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2713,8 +2713,8 @@ cir::FuncOp CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
f, mlir::SymbolTable::Visibility::Private);

// Initialize with empty dict of extra attributes.
f.setExtraAttrsAttr(cir::ExtraFuncAttributesAttr::get(
&getMLIRContext(), builder.getDictionaryAttr({})));
f.setExtraAttrsAttr(
cir::ExtraFuncAttributesAttr::get(builder.getDictionaryAttr({})));

if (!curCGF)
theModule.push_back(f);
Expand Down Expand Up @@ -2824,7 +2824,7 @@ void CIRGenModule::setCIRFunctionAttributesForDefinition(const Decl *decl,
}

f.setExtraAttrsAttr(cir::ExtraFuncAttributesAttr::get(
&getMLIRContext(), attrs.getDictionary(&getMLIRContext())));
attrs.getDictionary(&getMLIRContext())));
return;
}

Expand Down Expand Up @@ -2938,7 +2938,7 @@ void CIRGenModule::setCIRFunctionAttributesForDefinition(const Decl *decl,
}

f.setExtraAttrsAttr(cir::ExtraFuncAttributesAttr::get(
&getMLIRContext(), attrs.getDictionary(&getMLIRContext())));
attrs.getDictionary(&getMLIRContext())));

assert(!MissingFeatures::setFunctionAlignment());

Expand All @@ -2963,8 +2963,8 @@ void CIRGenModule::setCIRFunctionAttributes(GlobalDecl gd,
mlir::NamedAttrList pal{func.getExtraAttrs().getElements().getValue()};
constructAttributeList(func.getName(), info, gd, pal, callingConv, sideEffect,
/*AttrOnCallSite=*/false, isThunk);
func.setExtraAttrsAttr(cir::ExtraFuncAttributesAttr::get(
&getMLIRContext(), pal.getDictionary(&getMLIRContext())));
func.setExtraAttrsAttr(
cir::ExtraFuncAttributesAttr::get(pal.getDictionary(&getMLIRContext())));

// TODO(cir): Check X86_VectorCall incompatibility with WinARM64EC

Expand Down Expand Up @@ -4212,7 +4212,7 @@ cir::AnnotationAttr
CIRGenModule::emitAnnotateAttr(const clang::AnnotateAttr *aa) {
mlir::StringAttr annoGV = builder.getStringAttr(aa->getAnnotation());
mlir::ArrayAttr args = emitAnnotationArgs(aa);
return cir::AnnotationAttr::get(&getMLIRContext(), annoGV, args);
return cir::AnnotationAttr::get(annoGV, args);
}

void CIRGenModule::addGlobalAnnotations(const ValueDecl *d,
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenOpenCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ void CIRGenModule::genKernelArgMetadata(cir::FuncOp Fn, const FunctionDecl *FD,
mlir::NamedAttrList items{Fn.getExtraAttrs().getElements().getValue()};
auto oldValue = items.set(value.getMnemonic(), value);
if (oldValue != value) {
Fn.setExtraAttrsAttr(cir::ExtraFuncAttributesAttr::get(
&getMLIRContext(), builder.getDictionaryAttr(items)));
Fn.setExtraAttrsAttr(
cir::ExtraFuncAttributesAttr::get(builder.getDictionaryAttr(items)));
}
} else {
if (shouldEmitArgName)
Expand Down Expand Up @@ -246,7 +246,7 @@ void CIRGenFunction::emitKernelMetadata(const FunctionDecl *FD,
attrs.append(kernelMetadataAttr.getMnemonic(), kernelMetadataAttr);

Fn.setExtraAttrsAttr(cir::ExtraFuncAttributesAttr::get(
&getMLIRContext(), attrs.getDictionary(&getMLIRContext())));
attrs.getDictionary(&getMLIRContext())));
}

void CIRGenModule::emitOpenCLMetadata() {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/ConstantInitBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class ConstantAggregateBuilderBase {
void addPointer(cir::PointerType ptrTy, uint64_t value) {
auto val = mlir::IntegerAttr::get(
mlir::IntegerType::get(ptrTy.getContext(), 64), value);
add(cir::ConstPtrAttr::get(ptrTy.getContext(), ptrTy, val));
add(cir::ConstPtrAttr::get(ptrTy, val));
}

/// Add a bitcast of a value to a specific type.
Expand Down
14 changes: 6 additions & 8 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2463,18 +2463,16 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) {

if (parseGlobalDtorCtor("global_ctor", [&](std::optional<int> prio) {
cir::GlobalCtorAttr globalCtorAttr =
prio ? cir::GlobalCtorAttr::get(builder.getContext(), nameAttr,
*prio)
: cir::GlobalCtorAttr::get(builder.getContext(), nameAttr);
prio ? cir::GlobalCtorAttr::get(nameAttr, *prio)
: cir::GlobalCtorAttr::get(nameAttr);
state.addAttribute(getGlobalCtorAttrName(state.name), globalCtorAttr);
}).failed())
return failure();

if (parseGlobalDtorCtor("global_dtor", [&](std::optional<int> prio) {
cir::GlobalDtorAttr globalDtorAttr =
prio ? cir::GlobalDtorAttr::get(builder.getContext(), nameAttr,
*prio)
: cir::GlobalDtorAttr::get(builder.getContext(), nameAttr);
prio ? cir::GlobalDtorAttr::get(nameAttr, *prio)
: cir::GlobalDtorAttr::get(nameAttr);
state.addAttribute(getGlobalDtorAttrName(state.name), globalDtorAttr);
}).failed())
return failure();
Expand All @@ -2490,7 +2488,7 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) {
} else {
NamedAttrList empty;
extraAttrs = cir::ExtraFuncAttributesAttr::get(
builder.getContext(), empty.getDictionary(builder.getContext()));
empty.getDictionary(builder.getContext()));
}
state.addAttribute(getExtraAttrsAttrName(state.name), extraAttrs);

Expand Down Expand Up @@ -2969,7 +2967,7 @@ static ::mlir::ParseResult parseCallCommon(::mlir::OpAsmParser &parser,
} else {
NamedAttrList empty;
extraAttrs = cir::ExtraFuncAttributesAttr::get(
builder.getContext(), empty.getDictionary(builder.getContext()));
empty.getDictionary(builder.getContext()));
}
result.addAttribute(extraAttrsAttrName, extraAttrs);

Expand Down
8 changes: 4 additions & 4 deletions clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ FuncOp LoweringPreparePass::buildRuntimeFunction(
f, mlir::SymbolTable::Visibility::Private);
mlir::NamedAttrList attrs;
f.setExtraAttrsAttr(cir::ExtraFuncAttributesAttr::get(
builder.getContext(), attrs.getDictionary(builder.getContext())));
attrs.getDictionary(builder.getContext())));
}
return f;
}
Expand Down Expand Up @@ -1532,9 +1532,9 @@ void LoweringPreparePass::buildGlobalAnnotationValues() {
return;
mlir::ArrayAttr annotationValueArray =
mlir::ArrayAttr::get(theModule.getContext(), globalAnnotations);
theModule->setAttr(cir::CIRDialect::getGlobalAnnotationsAttrName(),
cir::GlobalAnnotationValuesAttr::get(
theModule.getContext(), annotationValueArray));
theModule->setAttr(
cir::CIRDialect::getGlobalAnnotationsAttrName(),
cir::GlobalAnnotationValuesAttr::get(annotationValueArray));
}

void LoweringPreparePass::runOnOp(Operation *op) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2162,7 +2162,7 @@ void CIRToLLVMFuncOpLowering::lowerFuncOpenCLKernelMetadata(
newExtraAttrs.push_back(entry);
}
extraAttrsEntry.setValue(cir::ExtraFuncAttributesAttr::get(
getContext(), newExtraAttrs.getDictionary(getContext())));
newExtraAttrs.getDictionary(getContext())));
}

mlir::LogicalResult CIRToLLVMFuncOpLowering::matchAndRewrite(
Expand Down
Loading