Skip to content

Commit c64b3f6

Browse files
committed
Allow negative constants in switch case values
Signed-off-by: Roberto Raggi <roberto.raggi@gmail.com>
1 parent 62a6850 commit c64b3f6

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

src/mlir/cxx/mlir/CxxOps.td

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -458,16 +458,6 @@ def Cxx_SwitchOp : Cxx_Op<"switch", [ AttrSizedOperandSegments, Terminator ]> {
458458
CArg<"BlockRange", "{}">:$caseDestinations,
459459
CArg<"ArrayRef<ValueRange>", "{}">:$caseOperands)>
460460
];
461-
462-
let extraClassDeclaration = [{
463-
auto getCaseOperands(unsigned index) -> OperandRange{
464-
return getCaseOperands()[index];
465-
}
466-
467-
auto getCaseOperandsMutable(unsigned index) -> MutableOperandRange {
468-
return getCaseOperandsMutable()[index];
469-
}
470-
}];
471461
}
472462

473463
//

src/mlir/cxx/mlir/cxx_dialect.cc

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,24 @@ struct detail::ClassTypeStorage : public TypeStorage {
4040
public:
4141
using KeyTy = StringRef;
4242

43-
explicit ClassTypeStorage(const KeyTy &key) : name_(key) {}
43+
explicit ClassTypeStorage(const KeyTy& key) : name_(key) {}
4444

4545
auto getName() -> StringRef const { return name_; }
4646
auto getBody() const -> ArrayRef<Type> { return body_; }
4747

48-
auto operator==(const KeyTy &key) const -> bool { return name_ == key; };
48+
auto operator==(const KeyTy& key) const -> bool { return name_ == key; };
4949

50-
static auto hashKey(const KeyTy &key) -> llvm::hash_code {
50+
static auto hashKey(const KeyTy& key) -> llvm::hash_code {
5151
return llvm::hash_value(key);
5252
}
5353

54-
static ClassTypeStorage *construct(TypeStorageAllocator &allocator,
55-
const KeyTy &key) {
54+
static ClassTypeStorage* construct(TypeStorageAllocator& allocator,
55+
const KeyTy& key) {
5656
return new (allocator.allocate<ClassTypeStorage>())
5757
ClassTypeStorage(allocator.copyInto(key));
5858
}
5959

60-
auto mutate(TypeStorageAllocator &allocator, ArrayRef<Type> body)
60+
auto mutate(TypeStorageAllocator& allocator, ArrayRef<Type> body)
6161
-> LogicalResult {
6262
if (isInitialized_) return success(body == getBody());
6363

@@ -79,7 +79,7 @@ struct CxxGenerateAliases : public OpAsmDialectInterface {
7979
public:
8080
using OpAsmDialectInterface::OpAsmDialectInterface;
8181

82-
auto getAlias(Type type, raw_ostream &os) const -> AliasResult override {
82+
auto getAlias(Type type, raw_ostream& os) const -> AliasResult override {
8383
if (auto intType = dyn_cast<IntegerType>(type)) {
8484
os << 'i' << intType.getWidth() << (intType.getIsSigned() ? 's' : 'u');
8585
return AliasResult::FinalAlias;
@@ -126,18 +126,18 @@ void CxxDialect::initialize() {
126126
addInterface<CxxGenerateAliases>();
127127
}
128128

129-
void FuncOp::print(OpAsmPrinter &p) {
129+
void FuncOp::print(OpAsmPrinter& p) {
130130
const auto isVariadic = getFunctionType().getVariadic();
131131
function_interface_impl::printFunctionOp(
132132
p, *this, isVariadic, getFunctionTypeAttrName(), getArgAttrsAttrName(),
133133
getResAttrsAttrName());
134134
}
135135

136-
auto FuncOp::parse(OpAsmParser &parser, OperationState &result) -> ParseResult {
136+
auto FuncOp::parse(OpAsmParser& parser, OperationState& result) -> ParseResult {
137137
auto funcTypeBuilder =
138-
[](Builder &builder, llvm::ArrayRef<Type> argTypes,
138+
[](Builder& builder, llvm::ArrayRef<Type> argTypes,
139139
ArrayRef<Type> results, function_interface_impl::VariadicFlag,
140-
std::string &) { return builder.getFunctionType(argTypes, results); };
140+
std::string&) { return builder.getFunctionType(argTypes, results); };
141141

142142
return function_interface_impl::parseFunctionOp(
143143
parser, result, false, getFunctionTypeAttrName(result.name),
@@ -163,17 +163,17 @@ auto StoreOp::verify() -> LogicalResult {
163163
return success();
164164
}
165165

166-
void SwitchOp::build(OpBuilder &builder, OperationState &result, Value value,
167-
Block *defaultDestination, ValueRange defaultOperands,
166+
void SwitchOp::build(OpBuilder& builder, OperationState& result, Value value,
167+
Block* defaultDestination, ValueRange defaultOperands,
168168
DenseIntElementsAttr caseValues,
169169
BlockRange caseDestinations,
170170
ArrayRef<ValueRange> caseOperands) {
171171
build(builder, result, value, defaultOperands, caseOperands, caseValues,
172172
defaultDestination, caseDestinations);
173173
}
174174

175-
void SwitchOp::build(OpBuilder &builder, OperationState &result, Value value,
176-
Block *defaultDestination, ValueRange defaultOperands,
175+
void SwitchOp::build(OpBuilder& builder, OperationState& result, Value value,
176+
Block* defaultDestination, ValueRange defaultOperands,
177177
ArrayRef<std::int64_t> caseValues,
178178
BlockRange caseDestinations,
179179
ArrayRef<ValueRange> caseOperands) {
@@ -195,7 +195,8 @@ void SwitchOp::build(OpBuilder &builder, OperationState &result, Value value,
195195
caseValuesAttr = mlir::cast<DenseIntElementsAttr>(
196196
DenseIntElementsAttr::get(shapeType, caseValues)
197197
.mapValues(elementTy, [&](APInt v) {
198-
return APInt(elementTy.getIntOrFloatBitWidth(), v.getZExtValue());
198+
return APInt(elementTy.getIntOrFloatBitWidth(), v.getZExtValue(),
199+
false, true);
199200
}));
200201
}
201202

@@ -209,15 +210,15 @@ auto FunctionType::clone(TypeRange inputs, TypeRange results) const
209210
getVariadic());
210211
}
211212

212-
auto ClassType::getNamed(MLIRContext *context, StringRef name) -> ClassType {
213+
auto ClassType::getNamed(MLIRContext* context, StringRef name) -> ClassType {
213214
return Base::get(context, name);
214215
}
215216

216217
auto ClassType::setBody(llvm::ArrayRef<Type> body) -> LogicalResult {
217218
Base::mutate(body);
218219
}
219220

220-
void ClassType::print(AsmPrinter &p) const {
221+
void ClassType::print(AsmPrinter& p) const {
221222
FailureOr<AsmPrinter::CyclicPrintReset> cyclicPrint;
222223

223224
p << "<";
@@ -242,7 +243,7 @@ void ClassType::print(AsmPrinter &p) const {
242243
p << '>';
243244
}
244245

245-
auto ClassType::parse(AsmParser &parser) -> Type {
246+
auto ClassType::parse(AsmParser& parser) -> Type {
246247
// todo: implement parsing for ClassType
247248
return {};
248249
}

src/parser/cxx/ast_interpreter_expressions.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,33 @@ auto ASTInterpreter::ExpressionVisitor::operator()(UnaryExpressionAST* ast)
931931
-> ExpressionResult {
932932
auto expressionResult = interp.expression(ast->expression);
933933

934+
switch (ast->op) {
935+
case TokenKind::T_MINUS: {
936+
if (expressionResult.has_value() &&
937+
control()->is_integral_or_unscoped_enum(ast->expression->type)) {
938+
const auto sz = memoryLayout()->sizeOf(ast->expression->type);
939+
940+
if (sz <= 4) {
941+
if (control()->is_unsigned(ast->expression->type)) {
942+
return toValue(-toUInt32(expressionResult.value()));
943+
}
944+
945+
return ExpressionResult(-toInt32(expressionResult.value()));
946+
}
947+
948+
if (control()->is_unsigned(ast->expression->type)) {
949+
return toValue(-toUInt64(expressionResult.value()));
950+
}
951+
952+
return ExpressionResult(-toInt64(expressionResult.value()));
953+
}
954+
break;
955+
}
956+
957+
default:
958+
break;
959+
} // switch
960+
934961
return ExpressionResult{std::nullopt};
935962
}
936963

0 commit comments

Comments
 (0)