@@ -98,7 +98,7 @@ static Type getI1SameShape(Type type) {
98
98
}
99
99
100
100
// ===----------------------------------------------------------------------===//
101
- // Printing, parsing and builder for LLVM::CmpOp.
101
+ // Printing, parsing, folding and builder for LLVM::CmpOp.
102
102
// ===----------------------------------------------------------------------===//
103
103
104
104
void ICmpOp::print (OpAsmPrinter &p) {
@@ -175,6 +175,42 @@ ParseResult FCmpOp::parse(OpAsmParser &parser, OperationState &result) {
175
175
return parseCmpOp<FCmpPredicate>(parser, result);
176
176
}
177
177
178
+ // / Returns a scalar or vector boolean attribute of the given type.
179
+ static Attribute getBoolAttribute (Type type, MLIRContext *ctx, bool value) {
180
+ auto boolAttr = BoolAttr::get (ctx, value);
181
+ ShapedType shapedType = dyn_cast<ShapedType>(type);
182
+ if (!shapedType)
183
+ return boolAttr;
184
+ return DenseElementsAttr::get (shapedType, boolAttr);
185
+ }
186
+
187
+ OpFoldResult ICmpOp::fold (FoldAdaptor adaptor) {
188
+ if (getPredicate () != ICmpPredicate::eq &&
189
+ getPredicate () != ICmpPredicate::ne)
190
+ return {};
191
+
192
+ // cmpi(eq/ne, x, x) -> true/false
193
+ if (getLhs () == getRhs ())
194
+ return getBoolAttribute (getType (), getContext (),
195
+ getPredicate () == ICmpPredicate::eq);
196
+
197
+ // cmpi(eq/ne, alloca, null) -> false/true
198
+ if (getLhs ().getDefiningOp <AllocaOp>() && getRhs ().getDefiningOp <NullOp>())
199
+ return getBoolAttribute (getType (), getContext (),
200
+ getPredicate () == ICmpPredicate::ne);
201
+
202
+ // cmpi(eq/ne, null, alloca) -> cmpi(eq/ne, alloca, null)
203
+ if (getLhs ().getDefiningOp <NullOp>() && getRhs ().getDefiningOp <AllocaOp>()) {
204
+ Value lhs = getLhs ();
205
+ Value rhs = getRhs ();
206
+ getLhsMutable ().assign (rhs);
207
+ getRhsMutable ().assign (lhs);
208
+ return getResult ();
209
+ }
210
+
211
+ return {};
212
+ }
213
+
178
214
// ===----------------------------------------------------------------------===//
179
215
// Printing, parsing and verification for LLVM::AllocaOp.
180
216
// ===----------------------------------------------------------------------===//
@@ -2443,7 +2479,7 @@ Region *LLVMFuncOp::getCallableRegion() {
2443
2479
}
2444
2480
2445
2481
// ===----------------------------------------------------------------------===//
2446
- // Verification for LLVM:: ConstantOp.
2482
+ // ConstantOp.
2447
2483
// ===----------------------------------------------------------------------===//
2448
2484
2449
2485
LogicalResult LLVM::ConstantOp::verify () {
@@ -2503,6 +2539,25 @@ LogicalResult LLVM::ConstantOp::verify() {
2503
2539
return success ();
2504
2540
}
2505
2541
2542
+ bool LLVM::ConstantOp::isBuildableWith (Attribute value, Type type) {
2543
+ // The value's type must be the same as the provided type.
2544
+ auto typedAttr = dyn_cast<TypedAttr>(value);
2545
+ if (!typedAttr || typedAttr.getType () != type || !isCompatibleType (type))
2546
+ return false ;
2547
+ // The value's type must be an LLVM compatible type.
2548
+ if (!isCompatibleType (type))
2549
+ return false ;
2550
+ // TODO: Add support for additional attributes kinds once needed.
2551
+ return isa<IntegerAttr, FloatAttr, ElementsAttr>(value);
2552
+ }
2553
+
2554
+ ConstantOp LLVM::ConstantOp::materialize (OpBuilder &builder, Attribute value,
2555
+ Type type, Location loc) {
2556
+ if (isBuildableWith (value, type))
2557
+ return builder.create <LLVM::ConstantOp>(loc, cast<TypedAttr>(value));
2558
+ return nullptr ;
2559
+ }
2560
+
2506
2561
// Constant op constant-folds to its value.
2507
2562
OpFoldResult LLVM::ConstantOp::fold (FoldAdaptor) { return getValue (); }
2508
2563
@@ -3097,11 +3152,7 @@ LogicalResult LLVMDialect::verifyRegionResultAttribute(Operation *op,
3097
3152
3098
3153
Operation *LLVMDialect::materializeConstant (OpBuilder &builder, Attribute value,
3099
3154
Type type, Location loc) {
3100
- // TODO: Accept more possible attributes. So far, only IntegerAttr may come
3101
- // up.
3102
- if (!isa<IntegerAttr>(value))
3103
- return nullptr ;
3104
- return builder.create <LLVM::ConstantOp>(loc, type, value);
3155
+ return LLVM::ConstantOp::materialize (builder, value, type, loc);
3105
3156
}
3106
3157
3107
3158
// ===----------------------------------------------------------------------===//
0 commit comments