Skip to content

Commit e90e366

Browse files
[CIR][NFC] Refactor GotoSolver with faster containers (#1676)
- Replace std::map with llvm::StringMap and std::vector with llvm::SmallVector for improved performance. - Preserve the behavior - Remove unused headers
1 parent 74f16f7 commit e90e366

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

clang/lib/CIR/Dialect/Transforms/GotoSolver.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#include "PassDetail.h"
2-
#include "mlir/Dialect/Func/IR/FuncOps.h"
3-
#include "mlir/IR/PatternMatch.h"
4-
#include "mlir/Support/LogicalResult.h"
52
#include "mlir/Transforms/DialectConversion.h"
6-
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
73
#include "clang/CIR/Dialect/IR/CIRDialect.h"
84
#include "clang/CIR/Dialect/Passes.h"
95

6+
#include "llvm/ADT/SmallVector.h"
7+
#include "llvm/ADT/StringMap.h"
8+
#include "llvm/ADT/StringRef.h"
109
#include "llvm/Support/TimeProfiler.h"
1110

1211
using namespace mlir;
@@ -23,12 +22,13 @@ struct GotoSolverPass : public GotoSolverBase<GotoSolverPass> {
2322
static void process(cir::FuncOp func) {
2423

2524
mlir::OpBuilder rewriter(func.getContext());
26-
std::map<std::string, Block *> labels;
27-
std::vector<cir::GotoOp> gotos;
25+
llvm::StringMap<Block *> labels;
26+
llvm::SmallVector<cir::GotoOp, 4> gotos;
2827

2928
func.getBody().walk([&](mlir::Operation *op) {
3029
if (auto lab = dyn_cast<cir::LabelOp>(op)) {
31-
labels.emplace(lab.getLabel().str(), lab->getBlock());
30+
// Will construct a string copy inplace. Safely erase the label
31+
labels.try_emplace(lab.getLabel(), lab->getBlock());
3232
lab.erase();
3333
} else if (auto goTo = dyn_cast<cir::GotoOp>(op)) {
3434
gotos.push_back(goTo);
@@ -38,7 +38,7 @@ static void process(cir::FuncOp func) {
3838
for (auto goTo : gotos) {
3939
mlir::OpBuilder::InsertionGuard guard(rewriter);
4040
rewriter.setInsertionPoint(goTo);
41-
auto dest = labels[goTo.getLabel().str()];
41+
Block *dest = labels[goTo.getLabel()];
4242
rewriter.create<cir::BrOp>(goTo.getLoc(), dest);
4343
goTo.erase();
4444
}

0 commit comments

Comments
 (0)