@@ -2555,6 +2555,24 @@ Region *LLVMFuncOp::getCallableRegion() {
2555
2555
return &getBody ();
2556
2556
}
2557
2557
2558
+ // ===----------------------------------------------------------------------===//
2559
+ // UndefOp.
2560
+ // ===----------------------------------------------------------------------===//
2561
+
2562
+ // / Fold an undef operation to a dedicated undef attribute.
2563
+ OpFoldResult LLVM::UndefOp::fold (FoldAdaptor) {
2564
+ return LLVM::UndefAttr::get (getContext ());
2565
+ }
2566
+
2567
+ // ===----------------------------------------------------------------------===//
2568
+ // PoisonOp.
2569
+ // ===----------------------------------------------------------------------===//
2570
+
2571
+ // / Fold a poison operation to a dedicated poison attribute.
2572
+ OpFoldResult LLVM::PoisonOp::fold (FoldAdaptor) {
2573
+ return LLVM::PoisonAttr::get (getContext ());
2574
+ }
2575
+
2558
2576
// ===----------------------------------------------------------------------===//
2559
2577
// ZeroOp.
2560
2578
// ===----------------------------------------------------------------------===//
@@ -2568,6 +2586,15 @@ LogicalResult LLVM::ZeroOp::verify() {
2568
2586
return success ();
2569
2587
}
2570
2588
2589
+ // / Fold a zero operation to a builtin zero attribute when possible and fall
2590
+ // / back to a dedicated zero attribute.
2591
+ OpFoldResult LLVM::ZeroOp::fold (FoldAdaptor) {
2592
+ OpFoldResult result = Builder (getContext ()).getZeroAttr (getType ());
2593
+ if (result)
2594
+ return result;
2595
+ return LLVM::ZeroAttr::get (getContext ());
2596
+ }
2597
+
2571
2598
// ===----------------------------------------------------------------------===//
2572
2599
// ConstantOp.
2573
2600
// ===----------------------------------------------------------------------===//
@@ -3271,11 +3298,18 @@ LogicalResult LLVMDialect::verifyRegionResultAttribute(Operation *op,
3271
3298
3272
3299
Operation *LLVMDialect::materializeConstant (OpBuilder &builder, Attribute value,
3273
3300
Type type, Location loc) {
3274
- // If this was folded from an llvm.mlir.addressof operation, it should be
3275
- // materialized as such.
3301
+ // If this was folded from an operation other than llvm.mlir.constant, it
3302
+ // should be materialized as such. Note that an llvm.mlir.zero may fold into
3303
+ // a builtin zero attribute and thus will materialize as a llvm.mlir.constant.
3276
3304
if (auto symbol = dyn_cast<FlatSymbolRefAttr>(value))
3277
3305
if (isa<LLVM::LLVMPointerType>(type))
3278
3306
return builder.create <LLVM::AddressOfOp>(loc, type, symbol);
3307
+ if (isa<LLVM::UndefAttr>(value))
3308
+ return builder.create <LLVM::UndefOp>(loc, type);
3309
+ if (isa<LLVM::PoisonAttr>(value))
3310
+ return builder.create <LLVM::PoisonOp>(loc, type);
3311
+ if (isa<LLVM::ZeroAttr>(value))
3312
+ return builder.create <LLVM::ZeroOp>(loc, type);
3279
3313
// Otherwise try materializing it as a regular llvm.mlir.constant op.
3280
3314
return LLVM::ConstantOp::materialize (builder, value, type, loc);
3281
3315
}
0 commit comments