@@ -107,54 +107,54 @@ LogicalResult SwitchEliminationPass::eliminateSwitch(P4HIR::ControlOp control,
107107 auto tableName = llvm::Twine (prefix, " _table" ).str ();
108108 (void )rewriter.create <P4HIR::TableOp>(
109109 loc, tableName, hiddenAttr,
110- [&](OpBuilder &tableBuilder , Location tableLoc) {
110+ [&](OpBuilder &b , Location tableLoc) {
111111 // Table key
112112 auto keyFuncType = P4HIR::FuncType::get (ctx, {condType});
113- auto tableKeyOp = tableBuilder .create <P4HIR::TableKeyOp>(
113+ auto tableKeyOp = b .create <P4HIR::TableKeyOp>(
114114 tableLoc, keyFuncType, ArrayRef<DictionaryAttr>{}, DictionaryAttr ());
115115 tableKeyOp.createEntryBlock ();
116116 {
117- IRRewriter ::InsertionGuard guard (rewriter );
118- rewriter .setInsertionPointToStart (&tableKeyOp.getBody ().front ());
119- rewriter .create <P4HIR::TableKeyEntryOp>(
117+ OpBuilder ::InsertionGuard guard (b );
118+ b .setInsertionPointToStart (&tableKeyOp.getBody ().front ());
119+ b .create <P4HIR::TableKeyEntryOp>(
120120 tableLoc, " exact" , tableKeyOp.getBody ().front ().getArgument (0 ),
121121 mlir::DictionaryAttr::get (ctx, {}));
122122 }
123123
124124 // Table actions
125- tableBuilder .create <P4HIR::TableActionsOp>(
125+ b .create <P4HIR::TableActionsOp>(
126126 tableLoc, DictionaryAttr (),
127- [&](OpBuilder &actionsBuilder , Location actionsLoc) {
127+ [&](OpBuilder &b , Location actionsLoc) {
128128 for (auto actionNameAttr : actionNameAttrs) {
129129 auto actionName = mlir::cast<StringAttr>(actionNameAttr);
130130 auto actionFuncType = P4HIR::FuncType::get (ctx, {});
131131 auto actionRef = FlatSymbolRefAttr::get (actionName);
132- actionsBuilder .create <P4HIR::TableActionOp>(
132+ b .create <P4HIR::TableActionOp>(
133133 actionsLoc, actionRef, actionFuncType,
134134 ArrayRef<DictionaryAttr>{}, DictionaryAttr (),
135- [&](OpBuilder &actionBodyBuilder , Block::BlockArgListType,
135+ [&](OpBuilder &b , Block::BlockArgListType,
136136 Location actionBodyLoc) {
137137 auto controlRef = SymbolRefAttr::get (
138138 ctx, control.getName (),
139139 {FlatSymbolRefAttr::get (ctx, actionName)});
140- actionBodyBuilder .create <P4HIR::CallOp>(
140+ b .create <P4HIR::CallOp>(
141141 actionBodyLoc, controlRef, ValueRange{});
142142 });
143143 }
144144 });
145145
146146 // Table default action
147- tableBuilder .create <P4HIR::TableDefaultActionOp>(
147+ b .create <P4HIR::TableDefaultActionOp>(
148148 tableLoc, false , DictionaryAttr (),
149- [&](OpBuilder &defaultBuilder , Location defaultLoc) {
149+ [&](OpBuilder &b , Location defaultLoc) {
150150 auto controlRef = SymbolRefAttr::get (
151151 ctx, control.getName (),
152152 {FlatSymbolRefAttr::get (ctx, defaultActionName)});
153- defaultBuilder .create <P4HIR::CallOp>(defaultLoc, controlRef, ValueRange{});
153+ b .create <P4HIR::CallOp>(defaultLoc, controlRef, ValueRange{});
154154 });
155155
156- // Table entries - iterate cases directly
157- tableBuilder .create <P4HIR::TableEntriesOp>(
156+ // Table entries
157+ b .create <P4HIR::TableEntriesOp>(
158158 tableLoc, true , DictionaryAttr (),
159159 [&](OpBuilder &b, Location entriesLoc) {
160160 size_t caseIndex = 0 ;
@@ -168,11 +168,11 @@ LogicalResult SwitchEliminationPass::eliminateSwitch(P4HIR::ControlOp control,
168168 tupleType, b.getArrayAttr ({valueAttr}));
169169 b.create <P4HIR::TableEntryOp>(
170170 entriesLoc, keyAttr, false , TypedAttr (), DictionaryAttr (),
171- [&](OpBuilder &entryBuilder , Location entryLoc) {
171+ [&](OpBuilder &b , Location entryLoc) {
172172 auto controlRef = SymbolRefAttr::get (
173173 ctx, control.getName (),
174174 {FlatSymbolRefAttr::get (ctx, actionName)});
175- entryBuilder .create <P4HIR::CallOp>(
175+ b .create <P4HIR::CallOp>(
176176 entryLoc, controlRef, ValueRange{});
177177 });
178178 }
@@ -181,7 +181,7 @@ LogicalResult SwitchEliminationPass::eliminateSwitch(P4HIR::ControlOp control,
181181 });
182182 });
183183
184- // Apply table directly with switch condition (no intermediate variable)
184+ // Apply table directly with switch condition
185185 rewriter.setInsertionPoint (switchOp);
186186 auto tableRef = SymbolRefAttr::get (ctx, control.getName (),
187187 {FlatSymbolRefAttr::get (ctx, tableName)});
@@ -191,42 +191,36 @@ LogicalResult SwitchEliminationPass::eliminateSwitch(P4HIR::ControlOp control,
191191 auto actionRunField = rewriter.create <P4HIR::StructExtractOp>(
192192 loc, tableApply.getResult (), " action_run" );
193193
194- // Build the new switch on action_run - iterate cases directly
194+ // Build the new switch on action_run
195195 (void )rewriter.create <P4HIR::SwitchOp>(
196196 loc, actionRunField.getResult (),
197- [&](OpBuilder &switchBuilder, Location switchLoc) {
198- size_t caseIndex = 0 ;
199- for (auto caseOp : switchOp.cases ()) {
200- if (caseOp.getKind () == P4HIR::CaseOpKind::Default)
201- continue ;
202- auto actionName = mlir::cast<StringAttr>(actionNameAttrs[caseIndex]);
203- auto enumMemberAttr = P4HIR::EnumFieldAttr::get (actionEnumType, actionName.getValue ());
204- switchBuilder.create <P4HIR::CaseOp>(
205- switchLoc, switchBuilder.getArrayAttr ({enumMemberAttr}),
206- P4HIR::CaseOpKind::Equal,
207- [&](OpBuilder &caseBuilder, Location caseLoc) {
197+ [&](OpBuilder &b, Location switchLoc) {
198+ auto cloneCase = [&](P4HIR::CaseOp origCase, StringAttr actionName,
199+ P4HIR::CaseOpKind kind) {
200+ auto enumMemberAttr = P4HIR::EnumFieldAttr::get (
201+ actionEnumType, actionName.getValue ());
202+ b.create <P4HIR::CaseOp>(
203+ switchLoc, b.getArrayAttr ({enumMemberAttr}), kind,
204+ [&](OpBuilder &b, Location caseLoc) {
208205 IRMapping mapper;
209- for (auto &op : caseOp.getCaseRegion ().front ().without_terminator ())
210- caseBuilder.clone (op, mapper);
211- caseBuilder.create <P4HIR::YieldOp>(caseLoc);
206+ for (auto &op : origCase.getCaseRegion ().front ())
207+ b.clone (op, mapper);
212208 });
213- ++caseIndex;
214- }
209+ };
215210
216- if (defaultCase) {
217- auto enumMemberAttr = P4HIR::EnumFieldAttr::get (actionEnumType, defaultActionName.getValue ());
218- switchBuilder.create <P4HIR::CaseOp>(
219- switchLoc, switchBuilder.getArrayAttr ({enumMemberAttr}),
220- P4HIR::CaseOpKind::Default,
221- [&](OpBuilder &caseBuilder, Location caseLoc) {
222- IRMapping mapper;
223- for (auto &op : defaultCase.getCaseRegion ().front ().without_terminator ())
224- caseBuilder.clone (op, mapper);
225- caseBuilder.create <P4HIR::YieldOp>(caseLoc);
226- });
211+ size_t caseIndex = 0 ;
212+ for (auto caseOp : switchOp.cases ()) {
213+ bool isDefault = caseOp.getKind () == P4HIR::CaseOpKind::Default;
214+ auto actionName = isDefault
215+ ? defaultActionName
216+ : mlir::cast<StringAttr>(actionNameAttrs[caseIndex]);
217+ cloneCase (caseOp, actionName,
218+ isDefault ? P4HIR::CaseOpKind::Default
219+ : P4HIR::CaseOpKind::Equal);
220+ if (!isDefault) ++caseIndex;
227221 }
228222
229- switchBuilder .create <P4HIR::YieldOp>(switchLoc);
223+ b .create <P4HIR::YieldOp>(switchLoc);
230224 });
231225
232226 rewriter.eraseOp (switchOp);
@@ -252,4 +246,4 @@ void SwitchEliminationPass::runOnOperation() {
252246
253247std::unique_ptr<Pass> P4::P4MLIR::createSwitchEliminationPass () {
254248 return std::make_unique<SwitchEliminationPass>();
255- }
249+ }
0 commit comments