-
Notifications
You must be signed in to change notification settings - Fork 25
Fix issue: annotations get optimized out. #328
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
Open
vaalfreja
wants to merge
2
commits into
p4lang:main
Choose a base branch
from
vaalfreja:anno
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1039,6 +1039,26 @@ LogicalResult P4HIR::ScopeOp::verify() { | |
| return success(); | ||
| } | ||
|
|
||
| void P4HIR::ScopeOp::getEffects( | ||
| SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>> &effects) { | ||
| if (auto annotations = getAnnotations()) { | ||
| bool isEmpty = | ||
| getScopeRegion().hasOneBlock() && llvm::hasSingleElement(getScopeRegion().front()); | ||
|
|
||
| if (llvm::any_of(annotations->getValue(), | ||
| [&](auto attr) { return attr.getName() != "atomic" || !isEmpty; })) { | ||
| effects.emplace_back(MemoryEffects::Write::get(), SideEffects::DefaultResource::get()); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| for (Region ®ion : getOperation()->getRegions()) | ||
| for (Block &block : region) | ||
| for (Operation &op : block) | ||
| if (auto opEffects = mlir::getEffectsRecursively(&op)) | ||
| effects.append(opEffects->begin(), opEffects->end()); | ||
| } | ||
|
|
||
| LogicalResult P4HIR::ScopeOp::canonicalize(P4HIR::ScopeOp op, PatternRewriter &rewriter) { | ||
| // Canonicalize scope: one without variables could be inlined | ||
| if (op.getOps<VariableOp>().empty() && op.getScopeRegion().hasOneBlock() && | ||
|
|
@@ -1294,6 +1314,69 @@ Block *P4HIR::IfOp::elseBlock() { | |
| P4HIR::YieldOp P4HIR::IfOp::elseYield() { return cast<P4HIR::YieldOp>(&elseBlock()->back()); } | ||
|
|
||
| namespace { | ||
|
|
||
| static std::optional<bool> getConstantBool(mlir::Value value) { | ||
| auto cst = value.getDefiningOp<P4HIR::ConstOp>(); | ||
| if (!cst) return std::nullopt; | ||
|
|
||
| auto boolAttr = mlir::dyn_cast<P4HIR::BoolAttr>(cst.getValue()); | ||
| if (!boolAttr) return std::nullopt; | ||
|
|
||
| return boolAttr.getValue(); | ||
| } | ||
|
|
||
| static bool isBranchHint(llvm::StringRef name) { return name == "likely" || name == "unlikely"; } | ||
|
|
||
| static mlir::DictionaryAttr filterBranchHints(mlir::DictionaryAttr annotations, | ||
| mlir::MLIRContext *ctx) { | ||
| if (!annotations) return nullptr; | ||
|
|
||
| auto attrs = annotations.getValue(); | ||
|
|
||
| if (llvm::none_of(attrs, [](auto attr) { return isBranchHint(attr.getName().getValue()); })) { | ||
| return annotations; | ||
| } | ||
|
|
||
| llvm::SmallVector<mlir::NamedAttribute> filtered; | ||
| llvm::copy_if(attrs, std::back_inserter(filtered), | ||
| [](auto attr) { return !isBranchHint(attr.getName().getValue()); }); | ||
| return filtered.empty() ? nullptr : mlir::DictionaryAttr::get(ctx, filtered); | ||
| } | ||
|
|
||
| static void inlineBlockAndReplaceOp(mlir::Block *block, mlir::Operation *op, | ||
| mlir::PatternRewriter &rewriter) { | ||
| mlir::Operation *terminator = block->getTerminator(); | ||
| mlir::ValueRange results = terminator->getOperands(); | ||
| rewriter.inlineBlockBefore(block, op, /*blockArgs=*/{}); | ||
| op->getNumResults() == 0 ? rewriter.eraseOp(op) : rewriter.replaceOp(op, results); | ||
| rewriter.eraseOp(terminator); | ||
| } | ||
|
|
||
| static mlir::LogicalResult foldConstantIfBranch(P4HIR::IfOp ifOp, mlir::Region &branchRegion, | ||
| mlir::Block *branchBlock, | ||
| mlir::DictionaryAttr branchAnnotations, | ||
| mlir::PatternRewriter &rewriter) { | ||
| if (!branchBlock) { | ||
| rewriter.eraseOp(ifOp); | ||
| return mlir::success(); | ||
| } | ||
|
|
||
| auto filteredAnnotations = filterBranchHints(branchAnnotations, rewriter.getContext()); | ||
| if (!filteredAnnotations) { | ||
| inlineBlockAndReplaceOp(branchBlock, ifOp, rewriter); | ||
| return mlir::success(); | ||
| } | ||
|
|
||
| auto scopeOp = rewriter.create<P4HIR::ScopeOp>(ifOp.getLoc(), filteredAnnotations, | ||
| [](mlir::OpBuilder &, mlir::Location) {}); | ||
| scopeOp.getScopeRegion().front().erase(); | ||
| rewriter.inlineRegionBefore(branchRegion, scopeOp.getScopeRegion(), | ||
| scopeOp.getScopeRegion().begin()); | ||
| ifOp.getNumResults() == 0 ? rewriter.eraseOp(ifOp) | ||
| : rewriter.replaceOp(ifOp, scopeOp.getResults()); | ||
| return mlir::success(); | ||
| } | ||
|
|
||
| struct RemoveEmptyElseBranch : public OpRewritePattern<P4HIR::IfOp> { | ||
| using OpRewritePattern<P4HIR::IfOp>::OpRewritePattern; | ||
|
|
||
|
|
@@ -1358,10 +1441,26 @@ struct InvertIfCondition : public OpRewritePattern<P4HIR::IfOp> { | |
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| struct FoldConstantIf : public OpRewritePattern<P4HIR::IfOp> { | ||
| using OpRewritePattern<P4HIR::IfOp>::OpRewritePattern; | ||
|
|
||
| LogicalResult matchAndRewrite(P4HIR::IfOp ifOp, PatternRewriter &rewriter) const override { | ||
| auto constValue = getConstantBool(ifOp.getCondition()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can use m_Constant pattern matcher here with |
||
| if (!constValue) return failure(); | ||
|
|
||
| return *constValue ? foldConstantIfBranch(ifOp, ifOp.getThenRegion(), ifOp.thenBlock(), | ||
| ifOp.getThenAnnotationsAttr(), rewriter) | ||
| : foldConstantIfBranch(ifOp, ifOp.getElseRegion(), ifOp.elseBlock(), | ||
| ifOp.getElseAnnotationsAttr(), rewriter); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| void P4HIR::IfOp::getCanonicalizationPatterns(RewritePatternSet &results, MLIRContext *context) { | ||
| results.add<RemoveEmptyElseBranch, RemoveEmptyThenBranch, InvertIfCondition>(context); | ||
| results.add<RemoveEmptyElseBranch, RemoveEmptyThenBranch, InvertIfCondition, FoldConstantIf>( | ||
| context); | ||
| } | ||
|
|
||
| static mlir::LogicalResult verifyReturnLike(mlir::Operation *op, bool mayHaveNoOperands) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'd probably need to have some common place / API for built-in annotations...