Skip to content

[mlir][llvm] Implement ConstantLike for ZeroOp, UndefOp, PoisonOp #93690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
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
21 changes: 21 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -1037,4 +1037,25 @@ def LLVM_TargetFeaturesAttr : LLVM_Attr<"TargetFeatures", "target_features">
let genVerifyDecl = 1;
}

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

/// Folded into from LLVM::UndefOp.
def LLVM_UndefAttr : LLVM_Attr<"Undef", "undef">;

//===----------------------------------------------------------------------===//
// PoisonAttr
//===----------------------------------------------------------------------===//

/// Folded into from LLVM::PoisonOp.
def LLVM_PoisonAttr : LLVM_Attr<"Poison", "poison">;

//===----------------------------------------------------------------------===//
// ZeroAttr
//===----------------------------------------------------------------------===//

/// Folded into from LLVM::ZeroOp.
def LLVM_ZeroAttr : LLVM_Attr<"Zero", "zero">;

#endif // LLVMIR_ATTRDEFS
9 changes: 6 additions & 3 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ def LLVM_NoneTokenOp
let assemblyFormat = "attr-dict `:` type($res)";
}

def LLVM_UndefOp : LLVM_Op<"mlir.undef", [Pure]>,
def LLVM_UndefOp : LLVM_Op<"mlir.undef", [Pure, ConstantLike]>,
LLVM_Builder<"$res = llvm::UndefValue::get($_resultType);"> {
let summary = "Creates an undefined value of LLVM dialect type.";
let description = [{
Expand All @@ -1541,9 +1541,10 @@ def LLVM_UndefOp : LLVM_Op<"mlir.undef", [Pure]>,
let results = (outs LLVM_Type:$res);
let builders = [LLVM_OneResultOpBuilder];
let assemblyFormat = "attr-dict `:` type($res)";
let hasFolder = 1;
}

def LLVM_PoisonOp : LLVM_Op<"mlir.poison", [Pure]>,
def LLVM_PoisonOp : LLVM_Op<"mlir.poison", [Pure, ConstantLike]>,
LLVM_Builder<"$res = llvm::PoisonValue::get($_resultType);"> {
let summary = "Creates a poison value of LLVM dialect type.";
let description = [{
Expand All @@ -1563,10 +1564,11 @@ def LLVM_PoisonOp : LLVM_Op<"mlir.poison", [Pure]>,
let results = (outs LLVM_Type:$res);
let builders = [LLVM_OneResultOpBuilder];
let assemblyFormat = "attr-dict `:` type($res)";
let hasFolder = 1;
}

def LLVM_ZeroOp
: LLVM_Op<"mlir.zero", [Pure]>,
: LLVM_Op<"mlir.zero", [Pure, ConstantLike]>,
LLVM_Builder<"$res = llvm::Constant::getNullValue($_resultType);">
{
let summary = "Creates a zero-initialized value of LLVM dialect type.";
Expand All @@ -1588,6 +1590,7 @@ def LLVM_ZeroOp
let builders = [LLVM_OneResultOpBuilder];
let assemblyFormat = "attr-dict `:` type($res)";
let hasVerifier = 1;
let hasFolder = 1;
}

def LLVM_ConstantOp
Expand Down
38 changes: 36 additions & 2 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2555,6 +2555,24 @@ Region *LLVMFuncOp::getCallableRegion() {
return &getBody();
}

//===----------------------------------------------------------------------===//
// UndefOp.
//===----------------------------------------------------------------------===//

/// Fold an undef operation to a dedicated undef attribute.
OpFoldResult LLVM::UndefOp::fold(FoldAdaptor) {
return LLVM::UndefAttr::get(getContext());
}

//===----------------------------------------------------------------------===//
// PoisonOp.
//===----------------------------------------------------------------------===//

/// Fold a poison operation to a dedicated poison attribute.
OpFoldResult LLVM::PoisonOp::fold(FoldAdaptor) {
return LLVM::PoisonAttr::get(getContext());
}

//===----------------------------------------------------------------------===//
// ZeroOp.
//===----------------------------------------------------------------------===//
Expand All @@ -2568,6 +2586,15 @@ LogicalResult LLVM::ZeroOp::verify() {
return success();
}

/// Fold a zero operation to a builtin zero attribute when possible and fall
/// back to a dedicated zero attribute.
OpFoldResult LLVM::ZeroOp::fold(FoldAdaptor) {
OpFoldResult result = Builder(getContext()).getZeroAttr(getType());
if (result)
return result;
return LLVM::ZeroAttr::get(getContext());
}

//===----------------------------------------------------------------------===//
// ConstantOp.
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -3271,11 +3298,18 @@ LogicalResult LLVMDialect::verifyRegionResultAttribute(Operation *op,

Operation *LLVMDialect::materializeConstant(OpBuilder &builder, Attribute value,
Type type, Location loc) {
// If this was folded from an llvm.mlir.addressof operation, it should be
// materialized as such.
// If this was folded from an operation other than llvm.mlir.constant, it
// should be materialized as such. Note that an llvm.mlir.zero may fold into
// a builtin zero attribute and thus will materialize as a llvm.mlir.constant.
if (auto symbol = dyn_cast<FlatSymbolRefAttr>(value))
if (isa<LLVM::LLVMPointerType>(type))
return builder.create<LLVM::AddressOfOp>(loc, type, symbol);
if (isa<LLVM::UndefAttr>(value))
return builder.create<LLVM::UndefOp>(loc, type);
if (isa<LLVM::PoisonAttr>(value))
return builder.create<LLVM::PoisonOp>(loc, type);
if (isa<LLVM::ZeroAttr>(value))
return builder.create<LLVM::ZeroOp>(loc, type);
// Otherwise try materializing it as a regular llvm.mlir.constant op.
return LLVM::ConstantOp::materialize(builder, value, type, loc);
}
Expand Down
Loading
Loading