Skip to content

Commit 25861df

Browse files
committed
refactor(conversion): add pushValue helper to reduce boilerplate in ForthToMemRef.cpp
1 parent a0011bd commit 25861df

3 files changed

Lines changed: 23 additions & 44 deletions

File tree

lib/Conversion/ForthToMemRef/ForthToMemRef.cpp

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ class ForthToMemRefTypeConverter : public TypeConverter {
4848
}
4949
};
5050

51+
/// Push an i64 value onto the stack. Returns the new stack pointer.
52+
static Value pushValue(Location loc, ConversionPatternRewriter &rewriter,
53+
Value memref, Value stackPtr, Value value) {
54+
Value one = rewriter.create<arith::ConstantIndexOp>(loc, 1);
55+
Value newSP = rewriter.create<arith::AddIOp>(loc, stackPtr, one);
56+
rewriter.create<memref::StoreOp>(loc, value, memref, newSP);
57+
return newSP;
58+
}
59+
5160
/// Conversion pattern for forth.stack operation.
5261
/// Allocates a memref<256xi64> on the stack and initializes SP to 0.
5362
struct StackOpConversion : public OpConversionPattern<forth::StackOp> {
@@ -88,14 +97,9 @@ struct LiteralOpConversion : public OpConversionPattern<forth::LiteralOp> {
8897
Value memref = inputStack[0];
8998
Value stackPtr = inputStack[1];
9099

91-
// Increment stack pointer
92-
Value one = rewriter.create<arith::ConstantIndexOp>(loc, 1);
93-
Value newSP = rewriter.create<arith::AddIOp>(loc, stackPtr, one);
94-
95-
// Store literal value at new SP position
96100
Value literalValue = rewriter.create<arith::ConstantOp>(
97101
loc, rewriter.getI64Type(), op.getValueAttr());
98-
rewriter.create<memref::StoreOp>(loc, literalValue, memref, newSP);
102+
Value newSP = pushValue(loc, rewriter, memref, stackPtr, literalValue);
99103

100104
rewriter.replaceOpWithMultiple(op, {{memref, newSP}});
101105
return success();
@@ -117,15 +121,9 @@ struct DupOpConversion : public OpConversionPattern<forth::DupOp> {
117121
Value memref = inputStack[0];
118122
Value stackPtr = inputStack[1];
119123

120-
// Load value at current SP
124+
// Load value at current SP and push duplicate
121125
Value topValue = rewriter.create<memref::LoadOp>(loc, memref, stackPtr);
122-
123-
// Increment SP
124-
Value one = rewriter.create<arith::ConstantIndexOp>(loc, 1);
125-
Value newSP = rewriter.create<arith::AddIOp>(loc, stackPtr, one);
126-
127-
// Store duplicate at new SP
128-
rewriter.create<memref::StoreOp>(loc, topValue, memref, newSP);
126+
Value newSP = pushValue(loc, rewriter, memref, stackPtr, topValue);
129127

130128
rewriter.replaceOpWithMultiple(op, {{memref, newSP}});
131129
return success();
@@ -202,16 +200,11 @@ struct OverOpConversion : public OpConversionPattern<forth::OverOp> {
202200
Value memref = inputStack[0];
203201
Value stackPtr = inputStack[1];
204202

205-
// Load second element (SP - 1)
203+
// Load second element (SP - 1) and push copy
206204
Value one = rewriter.create<arith::ConstantIndexOp>(loc, 1);
207205
Value spMinus1 = rewriter.create<arith::SubIOp>(loc, stackPtr, one);
208206
Value secondValue = rewriter.create<memref::LoadOp>(loc, memref, spMinus1);
209-
210-
// Increment SP
211-
Value newSP = rewriter.create<arith::AddIOp>(loc, stackPtr, one);
212-
213-
// Store second value at new SP
214-
rewriter.create<memref::StoreOp>(loc, secondValue, memref, newSP);
207+
Value newSP = pushValue(loc, rewriter, memref, stackPtr, secondValue);
215208

216209
rewriter.replaceOpWithMultiple(op, {{memref, newSP}});
217210
return success();
@@ -623,9 +616,7 @@ struct ParamRefOpConversion : public OpConversionPattern<forth::ParamRefOp> {
623616
loc, rewriter.getI64Type(), ptrIndex);
624617

625618
// Push onto stack
626-
Value one = rewriter.create<arith::ConstantIndexOp>(loc, 1);
627-
Value newSP = rewriter.create<arith::AddIOp>(loc, stackPtr, one);
628-
rewriter.create<memref::StoreOp>(loc, ptrI64, memref, newSP);
619+
Value newSP = pushValue(loc, rewriter, memref, stackPtr, ptrI64);
629620

630621
rewriter.replaceOpWithMultiple(op, {{memref, newSP}});
631622
return success();
@@ -723,18 +714,12 @@ struct IntrinsicOpConversion : public OpConversionPattern<ForthOp> {
723714
Value memref = inputStack[0];
724715
Value stackPtr = inputStack[1];
725716

726-
// Increment stack pointer
727-
Value one = rewriter.create<arith::ConstantIndexOp>(loc, 1);
728-
Value newSP = rewriter.create<arith::AddIOp>(loc, stackPtr, one);
729-
730-
// Create intrinsic op
717+
// Create intrinsic op and push onto stack
731718
Value intrinsicValue = rewriter.create<forth::IntrinsicOp>(
732719
loc, rewriter.getIndexType(), rewriter.getStringAttr(intrinsicName));
733720
Value intrinsicI64 = rewriter.create<arith::IndexCastOp>(
734721
loc, rewriter.getI64Type(), intrinsicValue);
735-
736-
// Store at new SP
737-
rewriter.create<memref::StoreOp>(loc, intrinsicI64, memref, newSP);
722+
Value newSP = pushValue(loc, rewriter, memref, stackPtr, intrinsicI64);
738723

739724
rewriter.replaceOpWithMultiple(op, {{memref, newSP}});
740725
return success();
@@ -771,11 +756,7 @@ struct GlobalIdOpConversion : public OpConversionPattern<forth::GlobalIdOp> {
771756
Value globalId = rewriter.create<arith::AddIOp>(loc, product, tidX);
772757
Value globalIdI64 = rewriter.create<arith::IndexCastOp>(
773758
loc, rewriter.getI64Type(), globalId);
774-
775-
// Increment SP and store result
776-
Value one = rewriter.create<arith::ConstantIndexOp>(loc, 1);
777-
Value newSP = rewriter.create<arith::AddIOp>(loc, stackPtr, one);
778-
rewriter.create<memref::StoreOp>(loc, globalIdI64, memref, newSP);
759+
Value newSP = pushValue(loc, rewriter, memref, stackPtr, globalIdI64);
779760

780761
rewriter.replaceOpWithMultiple(op, {{memref, newSP}});
781762
return success();
@@ -1100,10 +1081,8 @@ struct LoopIndexOpConversion : public OpConversionPattern<forth::LoopIndexOp> {
11001081
Value ivI64 =
11011082
rewriter.create<arith::IndexCastOp>(loc, rewriter.getI64Type(), iv);
11021083

1103-
// Push onto stack: SP += 1, store at new SP
1104-
Value one = rewriter.create<arith::ConstantIndexOp>(loc, 1);
1105-
Value newSP = rewriter.create<arith::AddIOp>(loc, stackPtr, one);
1106-
rewriter.create<memref::StoreOp>(loc, ivI64, memref, newSP);
1084+
// Push onto stack
1085+
Value newSP = pushValue(loc, rewriter, memref, stackPtr, ivI64);
11071086

11081087
rewriter.replaceOpWithMultiple(op, {{memref, newSP}});
11091088
return success();

test/Conversion/ForthToMemRef/control-flow.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212

1313
// Then branch: drop (subi) + literal push + yield
1414
// CHECK: arith.subi
15-
// CHECK: arith.addi
1615
// CHECK: arith.constant 42 : i64
16+
// CHECK: arith.addi
1717
// CHECK: memref.store
1818
// CHECK: scf.yield %{{.*}} : index
1919

2020
// Else branch: drop (subi) + literal push + yield
2121
// CHECK: } else {
2222
// CHECK: arith.subi
23-
// CHECK: arith.addi
2423
// CHECK: arith.constant 99 : i64
24+
// CHECK: arith.addi
2525
// CHECK: memref.store
2626
// CHECK: scf.yield %{{.*}} : index
2727
// CHECK: }

test/Conversion/ForthToMemRef/literal.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// CHECK-LABEL: func.func private @main
44
// CHECK: memref.alloca() : memref<256xi64>
55
// CHECK: arith.constant 0 : index
6+
// CHECK: arith.constant 42 : i64
67
// CHECK: arith.constant 1 : index
78
// CHECK: arith.addi %{{.*}}, %{{.*}} : index
8-
// CHECK: arith.constant 42 : i64
99
// CHECK: memref.store %{{.*}}, %{{.*}}[%{{.*}}] : memref<256xi64>
1010

1111
module {

0 commit comments

Comments
 (0)