diff --git a/lib/Dialect/FIRRTL/Import/FIRParser.cpp b/lib/Dialect/FIRRTL/Import/FIRParser.cpp index 7470717acab3..69dc43049589 100644 --- a/lib/Dialect/FIRRTL/Import/FIRParser.cpp +++ b/lib/Dialect/FIRRTL/Import/FIRParser.cpp @@ -1301,8 +1301,9 @@ struct FIRModuleContext : public FIRParser { llvm::DenseMap, Value> constantCache; /// Get a cached constant. - Value getCachedConstantInt(ImplicitLocOpBuilder &builder, Attribute attr, - IntType type, APInt &value) { + template + Value getCachedConstant(ImplicitLocOpBuilder &builder, Attribute attr, + Type type, Args &&...args) { auto &result = constantCache[{attr, type}]; if (result) return result; @@ -1312,15 +1313,15 @@ struct FIRModuleContext : public FIRParser { OpBuilder::InsertPoint savedIP; auto *parentOp = builder.getInsertionBlock()->getParentOp(); - if (!isa(parentOp)) { + if (!isa(parentOp)) { savedIP = builder.saveInsertionPoint(); - while (!isa(parentOp)) { + while (!isa(parentOp)) { builder.setInsertionPoint(parentOp); parentOp = builder.getInsertionBlock()->getParentOp(); } } - result = builder.create(type, value); + result = builder.create(type, std::forward(args)...); if (savedIP.isSet()) builder.setInsertionPoint(savedIP.getBlock(), savedIP.getPoint()); @@ -1913,8 +1914,9 @@ ParseResult FIRStmtParser::parseExpImpl(Value &result, const Twine &message, "expected string literal in String expression") || parseToken(FIRToken::r_paren, "expected ')' in String expression")) return failure(); - result = builder.create( - builder.getStringAttr(FIRToken::getStringValue(spelling))); + auto attr = builder.getStringAttr(FIRToken::getStringValue(spelling)); + result = moduleContext.getCachedConstant( + builder, attr, builder.getType(), attr); break; } case FIRToken::kw_Integer: { @@ -1927,8 +1929,10 @@ ParseResult FIRStmtParser::parseExpImpl(Value &result, const Twine &message, parseIntLit(value, "expected integer literal in Integer expression") || parseToken(FIRToken::r_paren, "expected ')' in Integer expression")) return failure(); - result = - builder.create(APSInt(value, /*isUnsigned=*/false)); + APSInt apint(value, /*isUnsigned=*/false); + result = moduleContext.getCachedConstant( + builder, IntegerAttr::get(getContext(), apint), + builder.getType(), apint); break; } case FIRToken::kw_Bool: { @@ -1947,7 +1951,9 @@ ParseResult FIRStmtParser::parseExpImpl(Value &result, const Twine &message, return emitError("expected true or false in Bool expression"); if (parseToken(FIRToken::r_paren, "expected ')' in Bool expression")) return failure(); - result = builder.create(value); + auto attr = builder.getBoolAttr(value); + result = moduleContext.getCachedConstant( + builder, attr, builder.getType(), value); break; } case FIRToken::kw_Double: { @@ -1967,7 +1973,9 @@ ParseResult FIRStmtParser::parseExpImpl(Value &result, const Twine &message, double d; if (!llvm::to_float(spelling, d)) return emitError("invalid double"); - result = builder.create(builder.getF64FloatAttr(d)); + auto attr = builder.getF64FloatAttr(d); + result = moduleContext.getCachedConstant( + builder, attr, builder.getType(), attr); break; } case FIRToken::kw_List: { @@ -2424,7 +2432,7 @@ ParseResult FIRStmtParser::parseIntegerLiteralExp(Value &result) { } locationProcessor.setLoc(loc); - result = moduleContext.getCachedConstantInt(builder, attr, type, value); + result = moduleContext.getCachedConstant(builder, attr, type, attr); return success(); } @@ -3749,7 +3757,7 @@ ParseResult FIRStmtParser::parseRefForceInitial() { value.getBitWidth(), IntegerType::Unsigned), value); - auto pred = moduleContext.getCachedConstantInt(builder, attr, type, value); + auto pred = moduleContext.getCachedConstant(builder, attr, type, attr); builder.create(pred, dest, src); return success(); @@ -3812,7 +3820,7 @@ ParseResult FIRStmtParser::parseRefReleaseInitial() { value.getBitWidth(), IntegerType::Unsigned), value); - auto pred = moduleContext.getCachedConstantInt(builder, attr, type, value); + auto pred = moduleContext.getCachedConstant(builder, attr, type, attr); builder.create(pred, dest); return success();