Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
[ghstack-poisoned]
  • Loading branch information
smeenai committed Oct 18, 2024
1 parent 7683e85 commit f1412a5
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 10 deletions.
15 changes: 15 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ def ZeroAttr : CIR_Attr<"Zero", "zero", [TypedAttrInterface]> {
let assemblyFormat = [{}];
}

//===----------------------------------------------------------------------===//
// UndefAttr
//===----------------------------------------------------------------------===//

def UndefAttr : CIR_Attr<"Undef", "undef", [TypedAttrInterface]> {
let summary = "Represent an undef constant";
let description = [{
The UndefAttr represents an undef constant, corresponding to LLVM's notion
of undef.
}];

let parameters = (ins AttributeSelfTypeParameter<"">:$type);
let assemblyFormat = [{}];
}

//===----------------------------------------------------------------------===//
// ConstArrayAttr
//===----------------------------------------------------------------------===//
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,14 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType,
return op->emitOpError("zero expects struct or array type");
}

if (isa<UndefAttr>(attrType)) {
// Per the LLVM LangRef, "Undefined values may be of any type (other than
// 'label' or 'void')". We don't have label types so we just check for void.
if (!::mlir::isa<::mlir::cir::VoidType>(opType))
return success();
return op->emitOpError("undef expects non-void type");
}

if (mlir::isa<mlir::cir::BoolAttr>(attrType)) {
if (!mlir::isa<mlir::cir::BoolType>(opType))
return op->emitOpError("result type (")
Expand Down
32 changes: 23 additions & 9 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,16 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, mlir::cir::ZeroAttr zeroAttr,
loc, converter->convertType(zeroAttr.getType()));
}

/// UndefAttr visitor.
inline mlir::Value
lowerCirAttrAsValue(mlir::Operation *parentOp, mlir::cir::UndefAttr undefAttr,
mlir::ConversionPatternRewriter &rewriter,
const mlir::TypeConverter *converter) {
auto loc = parentOp->getLoc();
return rewriter.create<mlir::LLVM::UndefOp>(
loc, converter->convertType(undefAttr.getType()));
}

/// ConstStruct visitor.
mlir::Value lowerCirAttrAsValue(mlir::Operation *parentOp,
mlir::cir::ConstStructAttr constStruct,
Expand Down Expand Up @@ -626,6 +636,8 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, mlir::Attribute attr,
return lowerCirAttrAsValue(parentOp, boolAttr, rewriter, converter);
if (const auto zeroAttr = mlir::dyn_cast<mlir::cir::ZeroAttr>(attr))
return lowerCirAttrAsValue(parentOp, zeroAttr, rewriter, converter);
if (const auto undefAttr = mlir::dyn_cast<mlir::cir::UndefAttr>(attr))
return lowerCirAttrAsValue(parentOp, undefAttr, rewriter, converter);
if (const auto globalAttr = mlir::dyn_cast<mlir::cir::GlobalViewAttr>(attr))
return lowerCirAttrAsValue(parentOp, globalAttr, rewriter, converter);
if (const auto vtableAttr = mlir::dyn_cast<mlir::cir::VTableAttr>(attr))
Expand Down Expand Up @@ -1594,7 +1606,8 @@ class CIRConstantLowering
// Fetch operation constant array initializer.

auto constArr = mlir::dyn_cast<mlir::cir::ConstArrayAttr>(op.getValue());
if (!constArr && !isa<mlir::cir::ZeroAttr>(op.getValue()))
if (!constArr &&
!isa<mlir::cir::ZeroAttr, mlir::cir::UndefAttr>(op.getValue()))
return op.emitError() << "array does not have a constant initializer";

std::optional<mlir::Attribute> denseAttr;
Expand Down Expand Up @@ -1626,8 +1639,9 @@ class CIRConstantLowering
return mlir::success();
} else if (auto strTy =
mlir::dyn_cast<mlir::cir::StructType>(op.getType())) {
if (auto zero = mlir::dyn_cast<mlir::cir::ZeroAttr>(op.getValue())) {
auto initVal = lowerCirAttrAsValue(op, zero, rewriter, typeConverter);
auto attr = op.getValue();
if (mlir::isa<mlir::cir::ZeroAttr, mlir::cir::UndefAttr>(attr)) {
auto initVal = lowerCirAttrAsValue(op, attr, rewriter, typeConverter);
rewriter.replaceAllUsesWith(op, initVal);
rewriter.eraseOp(op);
return mlir::success();
Expand Down Expand Up @@ -2315,11 +2329,11 @@ class CIRGlobalOpLowering
} else if (auto boolAttr =
mlir::dyn_cast<mlir::cir::BoolAttr>(init.value())) {
init = rewriter.getBoolAttr(boolAttr.getValue());
} else if (isa<mlir::cir::ZeroAttr, mlir::cir::ConstPtrAttr>(
init.value())) {
// TODO(cir): once LLVM's dialect has a proper zeroinitializer attribute
// this should be updated. For now, we use a custom op to initialize
// globals to zero.
} else if (isa<mlir::cir::ZeroAttr, mlir::cir::ConstPtrAttr,
mlir::cir::UndefAttr>(init.value())) {
// TODO(cir): once LLVM's dialect has proper equivalent attributes this
// should be updated. For now, we use a custom op to initialize globals
// to the appropriate value.
setupRegionInitializedLLVMGlobalOp(op, rewriter);
auto value =
lowerCirAttrAsValue(op, init.value(), rewriter, typeConverter);
Expand Down Expand Up @@ -2357,7 +2371,7 @@ class CIRGlobalOpLowering
lowerCirAttrAsValue(op, typeinfoAttr, rewriter, typeConverter));
return mlir::success();
} else {
op.emitError() << "usupported initializer '" << init.value() << "'";
op.emitError() << "unsupported initializer '" << init.value() << "'";
return mlir::failure();
}

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/Lowering/LoweringHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void convertToDenseElementsAttrImpl(
continue;
}

if (mlir::isa<mlir::cir::ZeroAttr>(eltAttr)) {
if (mlir::isa<mlir::cir::ZeroAttr, mlir::cir::UndefAttr>(eltAttr)) {
currentIndex += elementsSizeInCurrentDim;
continue;
}
Expand Down
2 changes: 2 additions & 0 deletions clang/test/CIR/Lowering/const.cir
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module {
// CHECK: llvm.mlir.constant(dense<[1.000000e+00, 2.000000e+00]> : tensor<2xf32>) : !llvm.array<2 x f32>
%4 = cir.const #cir.zero : !cir.array<!s32i x 3>
// CHECK: llvm.mlir.zero : !llvm.array<3 x i32>
%5 = cir.const #cir.undef : !cir.array<!s32i x 3>
// CHECK: llvm.mlir.undef : !llvm.array<3 x i32>
cir.return
}

Expand Down
7 changes: 7 additions & 0 deletions clang/test/CIR/Lowering/globals.cir
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ module {
cir.global common @comm = #cir.int<0> : !s32i
// MLIR: llvm.mlir.global common @comm(0 : i32) {addr_space = 0 : i32} : i32

cir.global external @undefStruct = #cir.undef : !ty_Bar
// MLIR: llvm.mlir.global external @undefStruct()
// MLIR: %0 = llvm.mlir.undef : !llvm.struct<"struct.Bar", (i32, i8)>
// MLIR: llvm.return %0 : !llvm.struct<"struct.Bar", (i32, i8)>
// MLIR: }
// LLVM: @undefStruct = global %struct.Bar undef

cir.global "private" internal @Handlers = #cir.const_array<[#cir.const_struct<{#cir.global_view<@myfun> : !cir.ptr<!cir.func<!void (!s32i)>>}> : !ty_anon2E1_]> : !cir.array<!ty_anon2E1_ x 1>
cir.func internal private @myfun(%arg0: !s32i) {
%0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init] {alignment = 4 : i64}
Expand Down

0 comments on commit f1412a5

Please sign in to comment.