Skip to content

Commit 13beabe

Browse files
authored
Revert "[mlir][llvm] Port overflowFlags to a native operation property" (#89344)
Reverts #89312 as it breaks all flang buildbots
1 parent 46957a1 commit 13beabe

File tree

11 files changed

+109
-165
lines changed

11 files changed

+109
-165
lines changed

mlir/include/mlir/Conversion/ArithCommon/AttrToLLVMConverter.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ convertArithFastMathAttrToLLVM(arith::FastMathFlagsAttr fmfAttr);
3131
LLVM::IntegerOverflowFlags
3232
convertArithOverflowFlagsToLLVM(arith::IntegerOverflowFlags arithFlags);
3333

34+
/// Creates an LLVM overflow attribute from a given arithmetic overflow
35+
/// attribute.
36+
LLVM::IntegerOverflowFlagsAttr
37+
convertArithOverflowAttrToLLVM(arith::IntegerOverflowFlagsAttr flagsAttr);
38+
3439
/// Creates an LLVM rounding mode enum value from a given arithmetic rounding
3540
/// mode enum value.
3641
LLVM::RoundingMode
@@ -67,9 +72,6 @@ class AttrConvertFastMathToLLVM {
6772
}
6873

6974
ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
70-
LLVM::IntegerOverflowFlags getOverflowFlags() const {
71-
return LLVM::IntegerOverflowFlags::none;
72-
}
7375

7476
private:
7577
NamedAttrList convertedAttr;
@@ -87,18 +89,19 @@ class AttrConvertOverflowToLLVM {
8789
// Get the name of the arith overflow attribute.
8890
StringRef arithAttrName = SourceOp::getIntegerOverflowAttrName();
8991
// Remove the source overflow attribute.
90-
if (auto arithAttr = dyn_cast_if_present<arith::IntegerOverflowFlagsAttr>(
91-
convertedAttr.erase(arithAttrName))) {
92-
overflowFlags = convertArithOverflowFlagsToLLVM(arithAttr.getValue());
92+
auto arithAttr = dyn_cast_if_present<arith::IntegerOverflowFlagsAttr>(
93+
convertedAttr.erase(arithAttrName));
94+
if (arithAttr) {
95+
StringRef targetAttrName = TargetOp::getIntegerOverflowAttrName();
96+
convertedAttr.set(targetAttrName,
97+
convertArithOverflowAttrToLLVM(arithAttr));
9398
}
9499
}
95100

96101
ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
97-
LLVM::IntegerOverflowFlags getOverflowFlags() const { return overflowFlags; }
98102

99103
private:
100104
NamedAttrList convertedAttr;
101-
LLVM::IntegerOverflowFlags overflowFlags = LLVM::IntegerOverflowFlags::none;
102105
};
103106

104107
template <typename SourceOp, typename TargetOp>
@@ -129,9 +132,6 @@ class AttrConverterConstrainedFPToLLVM {
129132
}
130133

131134
ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
132-
LLVM::IntegerOverflowFlags getOverflowFlags() const {
133-
return LLVM::IntegerOverflowFlags::none;
134-
}
135135

136136
private:
137137
NamedAttrList convertedAttr;

mlir/include/mlir/Conversion/LLVMCommon/Pattern.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,20 @@
1111

1212
#include "mlir/Conversion/LLVMCommon/MemRefBuilder.h"
1313
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
14-
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
1514
#include "mlir/Transforms/DialectConversion.h"
1615

1716
namespace mlir {
1817
class CallOpInterface;
1918

2019
namespace LLVM {
2120
namespace detail {
22-
/// Handle generically setting flags as native properties on LLVM operations.
23-
void setNativeProperties(Operation *op, IntegerOverflowFlags overflowFlags);
24-
2521
/// Replaces the given operation "op" with a new operation of type "targetOp"
2622
/// and given operands.
27-
LogicalResult oneToOneRewrite(
28-
Operation *op, StringRef targetOp, ValueRange operands,
29-
ArrayRef<NamedAttribute> targetAttrs,
30-
const LLVMTypeConverter &typeConverter, ConversionPatternRewriter &rewriter,
31-
IntegerOverflowFlags overflowFlags = IntegerOverflowFlags::none);
23+
LogicalResult oneToOneRewrite(Operation *op, StringRef targetOp,
24+
ValueRange operands,
25+
ArrayRef<NamedAttribute> targetAttrs,
26+
const LLVMTypeConverter &typeConverter,
27+
ConversionPatternRewriter &rewriter);
3228

3329
} // namespace detail
3430
} // namespace LLVM

mlir/include/mlir/Conversion/LLVMCommon/VectorPattern.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ LogicalResult handleMultidimensionalVectors(
5454
std::function<Value(Type, ValueRange)> createOperand,
5555
ConversionPatternRewriter &rewriter);
5656

57-
LogicalResult vectorOneToOneRewrite(
58-
Operation *op, StringRef targetOp, ValueRange operands,
59-
ArrayRef<NamedAttribute> targetAttrs,
60-
const LLVMTypeConverter &typeConverter, ConversionPatternRewriter &rewriter,
61-
IntegerOverflowFlags overflowFlags = IntegerOverflowFlags::none);
57+
LogicalResult vectorOneToOneRewrite(Operation *op, StringRef targetOp,
58+
ValueRange operands,
59+
ArrayRef<NamedAttribute> targetAttrs,
60+
const LLVMTypeConverter &typeConverter,
61+
ConversionPatternRewriter &rewriter);
6262
} // namespace detail
6363
} // namespace LLVM
6464

@@ -70,9 +70,6 @@ class AttrConvertPassThrough {
7070
AttrConvertPassThrough(SourceOp srcOp) : srcAttrs(srcOp->getAttrs()) {}
7171

7272
ArrayRef<NamedAttribute> getAttrs() const { return srcAttrs; }
73-
LLVM::IntegerOverflowFlags getOverflowFlags() const {
74-
return LLVM::IntegerOverflowFlags::none;
75-
}
7673

7774
private:
7875
ArrayRef<NamedAttribute> srcAttrs;
@@ -103,8 +100,7 @@ class VectorConvertToLLVMPattern : public ConvertOpToLLVMPattern<SourceOp> {
103100

104101
return LLVM::detail::vectorOneToOneRewrite(
105102
op, TargetOp::getOperationName(), adaptor.getOperands(),
106-
attrConvert.getAttrs(), *this->getTypeConverter(), rewriter,
107-
attrConvert.getOverflowFlags());
103+
attrConvert.getAttrs(), *this->getTypeConverter(), rewriter);
108104
}
109105
};
110106
} // namespace mlir

mlir/include/mlir/Dialect/LLVMIR/LLVMInterfaces.td

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,40 +50,58 @@ def FastmathFlagsInterface : OpInterface<"FastmathFlagsInterface"> {
5050

5151
def IntegerOverflowFlagsInterface : OpInterface<"IntegerOverflowFlagsInterface"> {
5252
let description = [{
53-
This interface defines an LLVM operation with integer overflow flags and
54-
provides a uniform API for accessing them.
53+
Access to op integer overflow flags.
5554
}];
5655

5756
let cppNamespace = "::mlir::LLVM";
5857

5958
let methods = [
60-
InterfaceMethod<[{
61-
Get the integer overflow flags for the operation.
62-
}], "IntegerOverflowFlags", "getOverflowFlags", (ins), [{}], [{
63-
return $_op.getProperties().overflowFlags;
64-
}]>,
65-
InterfaceMethod<[{
66-
Set the integer overflow flags for the operation.
67-
}], "void", "setOverflowFlags", (ins "IntegerOverflowFlags":$flags), [{}], [{
68-
$_op.getProperties().overflowFlags = flags;
69-
}]>,
70-
InterfaceMethod<[{
71-
Returns whether the operation has the No Unsigned Wrap keyword.
72-
}], "bool", "hasNoUnsignedWrap", (ins), [{}], [{
73-
return bitEnumContainsAll($_op.getOverflowFlags(),
74-
IntegerOverflowFlags::nuw);
75-
}]>,
76-
InterfaceMethod<[{
77-
Returns whether the operation has the No Signed Wrap keyword.
78-
}], "bool", "hasNoSignedWrap", (ins), [{}], [{
79-
return bitEnumContainsAll($_op.getOverflowFlags(),
80-
IntegerOverflowFlags::nsw);
81-
}]>,
82-
StaticInterfaceMethod<[{
83-
Get the attribute name of the overflow flags property.
84-
}], "StringRef", "getOverflowFlagsAttrName", (ins), [{}], [{
85-
return "overflowFlags";
86-
}]>,
59+
InterfaceMethod<
60+
/*desc=*/ "Returns an IntegerOverflowFlagsAttr attribute for the operation",
61+
/*returnType=*/ "IntegerOverflowFlagsAttr",
62+
/*methodName=*/ "getOverflowAttr",
63+
/*args=*/ (ins),
64+
/*methodBody=*/ [{}],
65+
/*defaultImpl=*/ [{
66+
auto op = cast<ConcreteOp>(this->getOperation());
67+
return op.getOverflowFlagsAttr();
68+
}]
69+
>,
70+
InterfaceMethod<
71+
/*desc=*/ "Returns whether the operation has the No Unsigned Wrap keyword",
72+
/*returnType=*/ "bool",
73+
/*methodName=*/ "hasNoUnsignedWrap",
74+
/*args=*/ (ins),
75+
/*methodBody=*/ [{}],
76+
/*defaultImpl=*/ [{
77+
auto op = cast<ConcreteOp>(this->getOperation());
78+
IntegerOverflowFlags flags = op.getOverflowFlagsAttr().getValue();
79+
return bitEnumContainsAll(flags, IntegerOverflowFlags::nuw);
80+
}]
81+
>,
82+
InterfaceMethod<
83+
/*desc=*/ "Returns whether the operation has the No Signed Wrap keyword",
84+
/*returnType=*/ "bool",
85+
/*methodName=*/ "hasNoSignedWrap",
86+
/*args=*/ (ins),
87+
/*methodBody=*/ [{}],
88+
/*defaultImpl=*/ [{
89+
auto op = cast<ConcreteOp>(this->getOperation());
90+
IntegerOverflowFlags flags = op.getOverflowFlagsAttr().getValue();
91+
return bitEnumContainsAll(flags, IntegerOverflowFlags::nsw);
92+
}]
93+
>,
94+
StaticInterfaceMethod<
95+
/*desc=*/ [{Returns the name of the IntegerOverflowFlagsAttr attribute
96+
for the operation}],
97+
/*returnType=*/ "StringRef",
98+
/*methodName=*/ "getIntegerOverflowAttrName",
99+
/*args=*/ (ins),
100+
/*methodBody=*/ [{}],
101+
/*defaultImpl=*/ [{
102+
return "overflowFlags";
103+
}]
104+
>
87105
];
88106
}
89107

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ class LLVM_IntArithmeticOpWithOverflowFlag<string mnemonic, string instName,
6060
LLVM_ArithmeticOpBase<AnySignlessInteger, mnemonic, instName,
6161
!listconcat([DeclareOpInterfaceMethods<IntegerOverflowFlagsInterface>], traits)> {
6262
dag iofArg = (
63-
ins EnumProperty<"IntegerOverflowFlags">:$overflowFlags);
63+
ins DefaultValuedAttr<LLVM_IntegerOverflowFlagsAttr, "{}">:$overflowFlags);
6464
let arguments = !con(commonArgs, iofArg);
6565
string mlirBuilder = [{
6666
auto op = $_builder.create<$_qualCppClassName>($_location, $lhs, $rhs);
67-
moduleImport.setIntegerOverflowFlags(inst, op);
67+
moduleImport.setIntegerOverflowFlagsAttr(inst, op);
6868
$res = op;
6969
}];
7070
let assemblyFormat = [{
71-
$lhs `,` $rhs `` custom<OverflowFlags>($overflowFlags)
72-
`` custom<LLVMOpAttrs>(attr-dict) `:` type($res)
71+
$lhs `,` $rhs (`overflow` `` $overflowFlags^)?
72+
custom<LLVMOpAttrs>(attr-dict) `:` type($res)
7373
}];
7474
string llvmBuilder =
7575
"$res = builder.Create" # instName #

mlir/include/mlir/Target/LLVMIR/ModuleImport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ class ModuleImport {
183183
/// Sets the integer overflow flags (nsw/nuw) attribute for the imported
184184
/// operation `op` given the original instruction `inst`. Asserts if the
185185
/// operation does not implement the integer overflow flag interface.
186-
void setIntegerOverflowFlags(llvm::Instruction *inst, Operation *op) const;
186+
void setIntegerOverflowFlagsAttr(llvm::Instruction *inst,
187+
Operation *op) const;
187188

188189
/// Sets the fastmath flags attribute for the imported operation `op` given
189190
/// the original instruction `inst`. Asserts if the operation does not

mlir/lib/Conversion/ArithCommon/AttrToLLVMConverter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ LLVM::IntegerOverflowFlags mlir::arith::convertArithOverflowFlagsToLLVM(
4949
return llvmFlags;
5050
}
5151

52+
LLVM::IntegerOverflowFlagsAttr mlir::arith::convertArithOverflowAttrToLLVM(
53+
arith::IntegerOverflowFlagsAttr flagsAttr) {
54+
arith::IntegerOverflowFlags arithFlags = flagsAttr.getValue();
55+
return LLVM::IntegerOverflowFlagsAttr::get(
56+
flagsAttr.getContext(), convertArithOverflowFlagsToLLVM(arithFlags));
57+
}
58+
5259
LLVM::RoundingMode
5360
mlir::arith::convertArithRoundingModeToLLVM(arith::RoundingMode roundingMode) {
5461
switch (roundingMode) {

mlir/lib/Conversion/LLVMCommon/Pattern.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -329,19 +329,14 @@ LogicalResult ConvertToLLVMPattern::copyUnrankedDescriptors(
329329
// Detail methods
330330
//===----------------------------------------------------------------------===//
331331

332-
void LLVM::detail::setNativeProperties(Operation *op,
333-
IntegerOverflowFlags overflowFlags) {
334-
if (auto iface = dyn_cast<IntegerOverflowFlagsInterface>(op))
335-
iface.setOverflowFlags(overflowFlags);
336-
}
337-
338332
/// Replaces the given operation "op" with a new operation of type "targetOp"
339333
/// and given operands.
340-
LogicalResult LLVM::detail::oneToOneRewrite(
341-
Operation *op, StringRef targetOp, ValueRange operands,
342-
ArrayRef<NamedAttribute> targetAttrs,
343-
const LLVMTypeConverter &typeConverter, ConversionPatternRewriter &rewriter,
344-
IntegerOverflowFlags overflowFlags) {
334+
LogicalResult
335+
LLVM::detail::oneToOneRewrite(Operation *op, StringRef targetOp,
336+
ValueRange operands,
337+
ArrayRef<NamedAttribute> targetAttrs,
338+
const LLVMTypeConverter &typeConverter,
339+
ConversionPatternRewriter &rewriter) {
345340
unsigned numResults = op->getNumResults();
346341

347342
SmallVector<Type> resultTypes;
@@ -357,8 +352,6 @@ LogicalResult LLVM::detail::oneToOneRewrite(
357352
rewriter.create(op->getLoc(), rewriter.getStringAttr(targetOp), operands,
358353
resultTypes, targetAttrs);
359354

360-
setNativeProperties(newOp, overflowFlags);
361-
362355
// If the operation produced 0 or 1 result, return them immediately.
363356
if (numResults == 0)
364357
return rewriter.eraseOp(op), success();

mlir/lib/Conversion/LLVMCommon/VectorPattern.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ LogicalResult LLVM::detail::handleMultidimensionalVectors(
103103
return success();
104104
}
105105

106-
LogicalResult LLVM::detail::vectorOneToOneRewrite(
107-
Operation *op, StringRef targetOp, ValueRange operands,
108-
ArrayRef<NamedAttribute> targetAttrs,
109-
const LLVMTypeConverter &typeConverter, ConversionPatternRewriter &rewriter,
110-
IntegerOverflowFlags overflowFlags) {
106+
LogicalResult
107+
LLVM::detail::vectorOneToOneRewrite(Operation *op, StringRef targetOp,
108+
ValueRange operands,
109+
ArrayRef<NamedAttribute> targetAttrs,
110+
const LLVMTypeConverter &typeConverter,
111+
ConversionPatternRewriter &rewriter) {
111112
assert(!operands.empty());
112113

113114
// Cannot convert ops if their operands are not of LLVM type.
@@ -117,15 +118,14 @@ LogicalResult LLVM::detail::vectorOneToOneRewrite(
117118
auto llvmNDVectorTy = operands[0].getType();
118119
if (!isa<LLVM::LLVMArrayType>(llvmNDVectorTy))
119120
return oneToOneRewrite(op, targetOp, operands, targetAttrs, typeConverter,
120-
rewriter, overflowFlags);
121+
rewriter);
121122

122-
auto callback = [op, targetOp, targetAttrs, overflowFlags,
123-
&rewriter](Type llvm1DVectorTy, ValueRange operands) {
124-
Operation *newOp =
125-
rewriter.create(op->getLoc(), rewriter.getStringAttr(targetOp),
126-
operands, llvm1DVectorTy, targetAttrs);
127-
LLVM::detail::setNativeProperties(newOp, overflowFlags);
128-
return newOp->getResult(0);
123+
auto callback = [op, targetOp, targetAttrs, &rewriter](Type llvm1DVectorTy,
124+
ValueRange operands) {
125+
return rewriter
126+
.create(op->getLoc(), rewriter.getStringAttr(targetOp), operands,
127+
llvm1DVectorTy, targetAttrs)
128+
->getResult(0);
129129
};
130130

131131
return handleMultidimensionalVectors(op, operands, typeConverter, callback,

0 commit comments

Comments
 (0)