Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIRRTL][FIRParser] Cache property constants. #7530

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions lib/Dialect/FIRRTL/Import/FIRParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,8 +1301,9 @@ struct FIRModuleContext : public FIRParser {
llvm::DenseMap<std::pair<Attribute, Type>, Value> constantCache;

/// Get a cached constant.
Value getCachedConstantInt(ImplicitLocOpBuilder &builder, Attribute attr,
IntType type, APInt &value) {
template <typename OpTy = ConstantOp, typename... Args>
Value getCachedConstant(ImplicitLocOpBuilder &builder, Attribute attr,
Type type, Args &&...args) {
auto &result = constantCache[{attr, type}];
if (result)
return result;
Expand All @@ -1312,15 +1313,15 @@ struct FIRModuleContext : public FIRParser {
OpBuilder::InsertPoint savedIP;

auto *parentOp = builder.getInsertionBlock()->getParentOp();
if (!isa<FModuleOp>(parentOp)) {
if (!isa<FModuleLike>(parentOp)) {
savedIP = builder.saveInsertionPoint();
while (!isa<FModuleOp>(parentOp)) {
while (!isa<FModuleLike>(parentOp)) {
builder.setInsertionPoint(parentOp);
parentOp = builder.getInsertionBlock()->getParentOp();
}
}

result = builder.create<ConstantOp>(type, value);
result = builder.create<OpTy>(type, std::forward<Args>(args)...);

if (savedIP.isSet())
builder.setInsertionPoint(savedIP.getBlock(), savedIP.getPoint());
Expand Down Expand Up @@ -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<StringConstantOp>(
builder.getStringAttr(FIRToken::getStringValue(spelling)));
auto attr = builder.getStringAttr(FIRToken::getStringValue(spelling));
result = moduleContext.getCachedConstant<StringConstantOp>(
builder, attr, builder.getType<StringType>(), attr);
break;
}
case FIRToken::kw_Integer: {
Expand All @@ -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<FIntegerConstantOp>(APSInt(value, /*isUnsigned=*/false));
APSInt apint(value, /*isUnsigned=*/false);
result = moduleContext.getCachedConstant<FIntegerConstantOp>(
builder, IntegerAttr::get(getContext(), apint),
builder.getType<FIntegerType>(), apint);
break;
}
case FIRToken::kw_Bool: {
Expand All @@ -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<BoolConstantOp>(value);
auto attr = builder.getBoolAttr(value);
result = moduleContext.getCachedConstant<BoolConstantOp>(
builder, attr, builder.getType<BoolType>(), value);
break;
}
case FIRToken::kw_Double: {
Expand All @@ -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<DoubleConstantOp>(builder.getF64FloatAttr(d));
auto attr = builder.getF64FloatAttr(d);
result = moduleContext.getCachedConstant<DoubleConstantOp>(
builder, attr, builder.getType<DoubleType>(), attr);
break;
}
case FIRToken::kw_List: {
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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<RefForceInitialOp>(pred, dest, src);

return success();
Expand Down Expand Up @@ -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<RefReleaseInitialOp>(pred, dest);

return success();
Expand Down
Loading