Skip to content

Commit ea55846

Browse files
committed
refactor : get rid of std::abs() & improve comments
1 parent 77800db commit ea55846

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

mlir/lib/Dialect/PDL/IR/Builtins.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,17 @@ LogicalResult static unaryOp(PatternRewriter &rewriter, PDLResultList &results,
107107
return failure();
108108

109109
results.push_back(rewriter.getIntegerAttr(
110-
integerType, std::abs(operandIntAttr.getSInt())));
110+
integerType, operandIntAttr.getValue().abs()));
111111
return success();
112112
}
113113
if (integerType.isSignless()) {
114+
// Overflow should not be checked.
115+
// Otherwise the purpose of signless integer is meaningless.
114116
results.push_back(rewriter.getIntegerAttr(
115-
integerType, std::abs(operandIntAttr.getInt())));
117+
integerType, operandIntAttr.getValue().abs()));
116118
return success();
117119
}
118-
// If unsigned, don't do anything
120+
// If unsigned, do nothing
119121
results.push_back(operandIntAttr);
120122
return success();
121123
} else {
@@ -163,9 +165,10 @@ LogicalResult static unaryOp(PatternRewriter &rewriter, PDLResultList &results,
163165
operandFloatAttr.getType(),
164166
(double)operandFloatAttr.getValue().getExactLog2()));
165167
} else if constexpr (T == UnaryOpKind::abs) {
166-
results.push_back(rewriter.getFloatAttr(
167-
operandFloatAttr.getType(),
168-
std::abs(operandFloatAttr.getValue().convertToFloat())));
168+
auto resultVal = operandFloatAttr.getValue();
169+
resultVal.clearSign();
170+
results.push_back(
171+
rewriter.getFloatAttr(operandFloatAttr.getType(), resultVal));
169172
} else {
170173
llvm::llvm_unreachable_internal(
171174
"encountered an unsupported unary operator");

mlir/lib/Tools/PDLL/Parser/Lexer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ class Token {
8181
equal_arrow,
8282
semicolon,
8383
/// Paired punctuation.
84+
add,
85+
sub,
8486
mul,
8587
div,
8688
mod,
87-
add,
88-
sub,
8989
log2,
9090
exp2,
9191
abs,

mlir/unittests/Dialect/PDL/BuiltinTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,19 @@ TEST_F(BuiltinTest, abs) {
752752
7);
753753
}
754754

755+
// signless integer: edge case -128
756+
// Overflow should not be checked
757+
// otherwise the purpose of signless integer is meaningless
758+
{
759+
auto value = rewriter.getI8IntegerAttr(-128);
760+
TestPDLResultList results(1);
761+
EXPECT_TRUE(builtin::abs(rewriter, results, {value}).succeeded());
762+
auto result = results.getResults()[0];
763+
EXPECT_EQ(
764+
cast<IntegerAttr>(result.cast<Attribute>()).getValue().getSExtValue(),
765+
-128);
766+
}
767+
755768
// float
756769
{
757770
auto value = rewriter.getF32FloatAttr(-1.0);

0 commit comments

Comments
 (0)