Skip to content

Commit 8dcdc0f

Browse files
authored
[CIR] Clean up IntAttr (#146661)
- Add common CIR_ prefix - Simplify printing/parsing - Make it use IntTypeInterface This mirrors incubator changes from llvm/clangir#1725
1 parent 38ad6b1 commit 8dcdc0f

File tree

5 files changed

+101
-75
lines changed

5 files changed

+101
-75
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
6363

6464
mlir::Value getConstAPInt(mlir::Location loc, mlir::Type typ,
6565
const llvm::APInt &val) {
66-
return create<cir::ConstantOp>(loc, getAttr<cir::IntAttr>(typ, val));
66+
return create<cir::ConstantOp>(loc, cir::IntAttr::get(typ, val));
6767
}
6868

6969
cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr) {

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,36 +117,67 @@ def UndefAttr : CIR_TypedAttr<"Undef", "undef"> {
117117
// IntegerAttr
118118
//===----------------------------------------------------------------------===//
119119

120-
def IntAttr : CIR_Attr<"Int", "int", [TypedAttrInterface]> {
120+
def CIR_IntAttr : CIR_Attr<"Int", "int", [TypedAttrInterface]> {
121121
let summary = "An attribute containing an integer value";
122122
let description = [{
123123
An integer attribute is a literal attribute that represents an integral
124124
value of the specified integer type.
125125
}];
126-
let parameters = (ins AttributeSelfTypeParameter<"">:$type,
127-
APIntParameter<"">:$value);
126+
127+
let parameters = (ins
128+
AttributeSelfTypeParameter<"", "cir::IntTypeInterface">:$type,
129+
APIntParameter<"">:$value
130+
);
131+
128132
let builders = [
129133
AttrBuilderWithInferredContext<(ins "mlir::Type":$type,
130134
"const llvm::APInt &":$value), [{
131-
return $_get(type.getContext(), type, value);
135+
auto intType = mlir::cast<cir::IntTypeInterface>(type);
136+
return $_get(type.getContext(), intType, value);
132137
}]>,
133138
AttrBuilderWithInferredContext<(ins "mlir::Type":$type,
134139
"int64_t":$value), [{
135-
IntType intType = mlir::cast<IntType>(type);
140+
auto intType = mlir::cast<cir::IntTypeInterface>(type);
136141
mlir::APInt apValue(intType.getWidth(), value, intType.isSigned());
137142
return $_get(intType.getContext(), intType, apValue);
138143
}]>,
139144
];
145+
140146
let extraClassDeclaration = [{
141-
int64_t getSInt() const { return getValue().getSExtValue(); }
142-
uint64_t getUInt() const { return getValue().getZExtValue(); }
143-
bool isNullValue() const { return getValue() == 0; }
144-
uint64_t getBitWidth() const {
145-
return mlir::cast<IntType>(getType()).getWidth();
147+
int64_t getSInt() const;
148+
uint64_t getUInt() const;
149+
bool isNullValue() const;
150+
bool isSigned() const;
151+
bool isUnsigned() const;
152+
uint64_t getBitWidth() const;
153+
}];
154+
155+
let extraClassDefinition = [{
156+
int64_t $cppClass::getSInt() const {
157+
return getValue().getSExtValue();
158+
}
159+
uint64_t $cppClass::getUInt() const {
160+
return getValue().getZExtValue();
161+
}
162+
bool $cppClass::isNullValue() const {
163+
return getValue() == 0;
164+
}
165+
bool $cppClass::isSigned() const {
166+
return mlir::cast<IntTypeInterface>(getType()).isSigned();
167+
}
168+
bool $cppClass::isUnsigned() const {
169+
return mlir::cast<IntTypeInterface>(getType()).isUnsigned();
170+
}
171+
uint64_t $cppClass::getBitWidth() const {
172+
return mlir::cast<IntTypeInterface>(getType()).getWidth();
146173
}
147174
}];
175+
176+
let assemblyFormat = [{
177+
`<` custom<IntLiteral>($value, ref($type)) `>`
178+
}];
179+
148180
let genVerifyDecl = 1;
149-
let hasCustomAssemblyFormat = 1;
150181
}
151182

152183
//===----------------------------------------------------------------------===//

clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ mlir::Attribute ConstantEmitter::tryEmitPrivate(const APValue &value,
684684
if (mlir::isa<cir::BoolType>(ty))
685685
return builder.getCIRBoolAttr(value.getInt().getZExtValue());
686686
assert(mlir::isa<cir::IntType>(ty) && "expected integral type");
687-
return cgm.getBuilder().getAttr<cir::IntAttr>(ty, value.getInt());
687+
return cir::IntAttr::get(ty, value.getInt());
688688
}
689689
case APValue::Float: {
690690
const llvm::APFloat &init = value.getFloat();
@@ -789,8 +789,8 @@ mlir::Attribute ConstantEmitter::tryEmitPrivate(const APValue &value,
789789
llvm::APSInt real = value.getComplexIntReal();
790790
llvm::APSInt imag = value.getComplexIntImag();
791791
return builder.getAttr<cir::ConstComplexAttr>(
792-
complexType, builder.getAttr<cir::IntAttr>(complexElemTy, real),
793-
builder.getAttr<cir::IntAttr>(complexElemTy, imag));
792+
complexType, cir::IntAttr::get(complexElemTy, real),
793+
cir::IntAttr::get(complexElemTy, imag));
794794
}
795795

796796
assert(isa<cir::FPTypeInterface>(complexElemTy) &&

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
157157
mlir::Value VisitIntegerLiteral(const IntegerLiteral *e) {
158158
mlir::Type type = cgf.convertType(e->getType());
159159
return builder.create<cir::ConstantOp>(
160-
cgf.getLoc(e->getExprLoc()),
161-
builder.getAttr<cir::IntAttr>(type, e->getValue()));
160+
cgf.getLoc(e->getExprLoc()), cir::IntAttr::get(type, e->getValue()));
162161
}
163162

164163
mlir::Value VisitFloatingLiteral(const FloatingLiteral *e) {
@@ -1970,21 +1969,21 @@ mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
19701969
"sizeof operator for VariableArrayType",
19711970
e->getStmtClassName());
19721971
return builder.getConstant(
1973-
loc, builder.getAttr<cir::IntAttr>(
1974-
cgf.cgm.UInt64Ty, llvm::APSInt(llvm::APInt(64, 1), true)));
1972+
loc, cir::IntAttr::get(cgf.cgm.UInt64Ty,
1973+
llvm::APSInt(llvm::APInt(64, 1), true)));
19751974
}
19761975
} else if (e->getKind() == UETT_OpenMPRequiredSimdAlign) {
19771976
cgf.getCIRGenModule().errorNYI(
19781977
e->getSourceRange(), "sizeof operator for OpenMpRequiredSimdAlign",
19791978
e->getStmtClassName());
19801979
return builder.getConstant(
1981-
loc, builder.getAttr<cir::IntAttr>(
1982-
cgf.cgm.UInt64Ty, llvm::APSInt(llvm::APInt(64, 1), true)));
1980+
loc, cir::IntAttr::get(cgf.cgm.UInt64Ty,
1981+
llvm::APSInt(llvm::APInt(64, 1), true)));
19831982
}
19841983

19851984
return builder.getConstant(
1986-
loc, builder.getAttr<cir::IntAttr>(
1987-
cgf.cgm.UInt64Ty, e->EvaluateKnownConstInt(cgf.getContext())));
1985+
loc, cir::IntAttr::get(cgf.cgm.UInt64Ty,
1986+
e->EvaluateKnownConstInt(cgf.getContext())));
19881987
}
19891988

19901989
/// Return true if the specified expression is cheap enough and side-effect-free

clang/lib/CIR/Dialect/IR/CIRAttrs.cpp

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515
#include "mlir/IR/DialectImplementation.h"
1616
#include "llvm/ADT/TypeSwitch.h"
1717

18+
//===-----------------------------------------------------------------===//
19+
// IntLiteral
20+
//===-----------------------------------------------------------------===//
21+
22+
static void printIntLiteral(mlir::AsmPrinter &p, llvm::APInt value,
23+
cir::IntTypeInterface ty);
24+
static mlir::ParseResult parseIntLiteral(mlir::AsmParser &parser,
25+
llvm::APInt &value,
26+
cir::IntTypeInterface ty);
27+
//===-----------------------------------------------------------------===//
28+
// FloatLiteral
29+
//===-----------------------------------------------------------------===//
30+
1831
static void printFloatLiteral(mlir::AsmPrinter &p, llvm::APFloat value,
1932
mlir::Type ty);
2033
static mlir::ParseResult
@@ -82,69 +95,52 @@ static void printConstPtr(AsmPrinter &p, mlir::IntegerAttr value) {
8295
// IntAttr definitions
8396
//===----------------------------------------------------------------------===//
8497

85-
Attribute IntAttr::parse(AsmParser &parser, Type odsType) {
86-
mlir::APInt apValue;
87-
88-
if (!mlir::isa<IntType>(odsType))
89-
return {};
90-
auto type = mlir::cast<IntType>(odsType);
91-
92-
// Consume the '<' symbol.
93-
if (parser.parseLess())
94-
return {};
95-
96-
// Fetch arbitrary precision integer value.
97-
if (type.isSigned()) {
98-
int64_t value = 0;
99-
if (parser.parseInteger(value)) {
100-
parser.emitError(parser.getCurrentLocation(), "expected integer value");
101-
} else {
102-
apValue = mlir::APInt(type.getWidth(), value, type.isSigned(),
103-
/*implicitTrunc=*/true);
104-
if (apValue.getSExtValue() != value)
105-
parser.emitError(parser.getCurrentLocation(),
106-
"integer value too large for the given type");
107-
}
98+
template <typename IntT>
99+
static bool isTooLargeForType(const mlir::APInt &value, IntT expectedValue) {
100+
if constexpr (std::is_signed_v<IntT>) {
101+
return value.getSExtValue() != expectedValue;
108102
} else {
109-
uint64_t value = 0;
110-
if (parser.parseInteger(value)) {
111-
parser.emitError(parser.getCurrentLocation(), "expected integer value");
112-
} else {
113-
apValue = mlir::APInt(type.getWidth(), value, type.isSigned(),
114-
/*implicitTrunc=*/true);
115-
if (apValue.getZExtValue() != value)
116-
parser.emitError(parser.getCurrentLocation(),
117-
"integer value too large for the given type");
118-
}
103+
return value.getZExtValue() != expectedValue;
119104
}
105+
}
120106

121-
// Consume the '>' symbol.
122-
if (parser.parseGreater())
123-
return {};
107+
template <typename IntT>
108+
static mlir::ParseResult parseIntLiteralImpl(mlir::AsmParser &p,
109+
llvm::APInt &value,
110+
cir::IntTypeInterface ty) {
111+
IntT ivalue;
112+
const bool isSigned = ty.isSigned();
113+
if (p.parseInteger(ivalue))
114+
return p.emitError(p.getCurrentLocation(), "expected integer value");
115+
116+
value = mlir::APInt(ty.getWidth(), ivalue, isSigned, /*implicitTrunc=*/true);
117+
if (isTooLargeForType(value, ivalue))
118+
return p.emitError(p.getCurrentLocation(),
119+
"integer value too large for the given type");
124120

125-
return IntAttr::get(type, apValue);
121+
return success();
122+
}
123+
124+
mlir::ParseResult parseIntLiteral(mlir::AsmParser &parser, llvm::APInt &value,
125+
cir::IntTypeInterface ty) {
126+
if (ty.isSigned())
127+
return parseIntLiteralImpl<int64_t>(parser, value, ty);
128+
return parseIntLiteralImpl<uint64_t>(parser, value, ty);
126129
}
127130

128-
void IntAttr::print(AsmPrinter &printer) const {
129-
auto type = mlir::cast<IntType>(getType());
130-
printer << '<';
131-
if (type.isSigned())
132-
printer << getSInt();
131+
void printIntLiteral(mlir::AsmPrinter &p, llvm::APInt value,
132+
cir::IntTypeInterface ty) {
133+
if (ty.isSigned())
134+
p << value.getSExtValue();
133135
else
134-
printer << getUInt();
135-
printer << '>';
136+
p << value.getZExtValue();
136137
}
137138

138139
LogicalResult IntAttr::verify(function_ref<InFlightDiagnostic()> emitError,
139-
Type type, APInt value) {
140-
if (!mlir::isa<IntType>(type))
141-
return emitError() << "expected 'simple.int' type";
142-
143-
auto intType = mlir::cast<IntType>(type);
144-
if (value.getBitWidth() != intType.getWidth())
140+
cir::IntTypeInterface type, llvm::APInt value) {
141+
if (value.getBitWidth() != type.getWidth())
145142
return emitError() << "type and value bitwidth mismatch: "
146-
<< intType.getWidth() << " != " << value.getBitWidth();
147-
143+
<< type.getWidth() << " != " << value.getBitWidth();
148144
return success();
149145
}
150146

0 commit comments

Comments
 (0)