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

[RTG][Elaboration] Move ConstantLike check after TypeSwitch for better performance #7998

Open
wants to merge 1 commit into
base: maerhart-rtg-elaboration-perf
Choose a base branch
from
Open
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
44 changes: 22 additions & 22 deletions lib/Dialect/RTG/Transforms/ElaborationPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,28 @@ class Elaborator
}

FailureOr<DeletionKind> visitExternalOp(Operation *op, VisitorInfo &info) {
if (op->hasTrait<OpTrait::ConstantLike>()) {
SmallVector<OpFoldResult, 1> result;
auto foldResult = op->fold(result);
(void)foldResult; // Make sure there is a user when assertions are off.
assert(succeeded(foldResult) &&
"constant folder of a constant-like must always succeed");
auto attr = dyn_cast<TypedAttr>(result[0].dyn_cast<Attribute>());
if (!attr)
return op->emitError(
"only typed attributes supported for constant-like operations");

auto intAttr = dyn_cast<IntegerAttr>(attr);
if (intAttr && isa<IndexType>(attr.getType()))
store(op->getResult(0), IndexValue(intAttr.getInt()));
else if (intAttr && intAttr.getType().isSignlessInteger(1))
store(op->getResult(0), BoolValue(bool(intAttr.getInt())));
else
store(op->getResult(0), AttributeValue(attr));

return DeletionKind::Delete;
}

// TODO: we only have this to be able to write tests for this pass without
// having to add support for more operations for now, so it should be
// removed once it is not necessary anymore for writing tests
Expand Down Expand Up @@ -1010,28 +1032,6 @@ class Elaborator
}

FailureOr<DeletionKind> dispatchOpVisitor(Operation *op, VisitorInfo &info) {
if (op->hasTrait<OpTrait::ConstantLike>()) {
SmallVector<OpFoldResult, 1> result;
auto foldResult = op->fold(result);
(void)foldResult; // Make sure there is a user when assertions are off.
assert(succeeded(foldResult) &&
"constant folder of a constant-like must always succeed");
auto attr = dyn_cast<TypedAttr>(result[0].dyn_cast<Attribute>());
if (!attr)
return op->emitError(
"only typed attributes supported for constant-like operations");

auto intAttr = dyn_cast<IntegerAttr>(attr);
if (intAttr && isa<IndexType>(attr.getType()))
store(op->getResult(0), IndexValue(intAttr.getInt()));
else if (intAttr && intAttr.getType().isSignlessInteger(1))
store(op->getResult(0), BoolValue(bool(intAttr.getInt())));
else
store(op->getResult(0), AttributeValue(attr));

return DeletionKind::Delete;
}

return TypeSwitch<Operation *, FailureOr<DeletionKind>>(op)
.Case<
// Index ops
Expand Down
Loading