Skip to content

Commit a820db9

Browse files
committed
feat: added fast math flags on MIR instructions
1 parent 2370907 commit a820db9

3 files changed

Lines changed: 59 additions & 59 deletions

File tree

compiler/astoir_mir/src/builder.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,52 +81,52 @@ pub fn build_upcast_float(ctx: &mut MIRContext, val: MIRFloatValue, size: usize)
8181
return res.as_float();
8282
}
8383

84-
pub fn build_int_add(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool) -> DiagnosticResult<MIRIntValue> {
84+
pub fn build_int_add(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool, fast: bool) -> DiagnosticResult<MIRIntValue> {
8585
if left.size != right.size {
8686
unsure_panic!("Tried using iadd on different sized integers");
8787
}
8888

89-
let res = ctx.append_inst(MIRInstruction::IntegerAdd { signed, left, right }).get()?;
89+
let res = ctx.append_inst(MIRInstruction::IntegerAdd { signed, fast, left, right }).get()?;
9090

9191
return res.as_int();
9292
}
9393

94-
pub fn build_int_sub(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool) -> DiagnosticResult<MIRIntValue> {
94+
pub fn build_int_sub(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool, fast: bool) -> DiagnosticResult<MIRIntValue> {
9595
if left.size != right.size {
9696
unsure_panic!("Tried using isub on different sized integers");
9797
}
9898

99-
let res = ctx.append_inst(MIRInstruction::IntegerSub { signed, left, right }).get()?;
99+
let res = ctx.append_inst(MIRInstruction::IntegerSub { signed, fast, left, right }).get()?;
100100

101101
return res.as_int();
102102
}
103103

104-
pub fn build_int_mul(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool) -> DiagnosticResult<MIRIntValue> {
104+
pub fn build_int_mul(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool, fast: bool) -> DiagnosticResult<MIRIntValue> {
105105
if left.size != right.size {
106106
unsure_panic!("Tried using imul on different sized integers");
107107
}
108108

109-
let res = ctx.append_inst(MIRInstruction::IntegerMul { signed, left, right }).get()?;
109+
let res = ctx.append_inst(MIRInstruction::IntegerMul { signed, fast, left, right }).get()?;
110110

111111
return res.as_int();
112112
}
113113

114-
pub fn build_int_div(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool) -> DiagnosticResult<MIRIntValue> {
114+
pub fn build_int_div(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool, fast: bool) -> DiagnosticResult<MIRIntValue> {
115115
if left.size != right.size {
116116
unsure_panic!("Tried using idiv on different sized integers");
117117
}
118118

119-
let res = ctx.append_inst(MIRInstruction::IntegerDiv { signed, left, right }).get()?;
119+
let res = ctx.append_inst(MIRInstruction::IntegerDiv { signed, fast, left, right }).get()?;
120120

121121
return res.as_int();
122122
}
123123

124-
pub fn build_int_mod(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool) -> DiagnosticResult<MIRIntValue> {
124+
pub fn build_int_mod(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool, fast: bool) -> DiagnosticResult<MIRIntValue> {
125125
if left.size != right.size {
126126
unsure_panic!("Tried using imod on different sized integers");
127127
}
128128

129-
let res = ctx.append_inst(MIRInstruction::IntegerMod { signed, left, right }).get()?;
129+
let res = ctx.append_inst(MIRInstruction::IntegerMod { signed, fast, left, right }).get()?;
130130

131131
return res.as_int();
132132
}
@@ -149,54 +149,54 @@ pub fn build_int_neg(ctx: &mut MIRContext, val: MIRIntValue) -> DiagnosticResult
149149
return res.as_int();
150150
}
151151

152-
pub fn build_float_add(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool) -> DiagnosticResult<MIRFloatValue> {
152+
pub fn build_float_add(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool, fast: bool) -> DiagnosticResult<MIRFloatValue> {
153153
if left.size != right.size {
154154
unsure_panic!("Tried using fadd on different sized integers");
155155
}
156156

157-
let res = ctx.append_inst(MIRInstruction::FloatAdd { signed, left, right }).get()?;
157+
let res = ctx.append_inst(MIRInstruction::FloatAdd { signed, fast, left, right }).get()?;
158158

159159
return res.as_float();
160160
}
161161

162-
pub fn build_float_sub(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool) -> DiagnosticResult<MIRFloatValue> {
162+
pub fn build_float_sub(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool, fast: bool) -> DiagnosticResult<MIRFloatValue> {
163163
if left.size != right.size {
164164
unsure_panic!("Tried using fsub on different sized integers");
165165
}
166166

167-
let res = ctx.append_inst(MIRInstruction::FloatSub { signed, left, right }).get()?;
167+
let res = ctx.append_inst(MIRInstruction::FloatSub { signed, fast, left, right }).get()?;
168168

169169
return res.as_float();
170170
}
171171

172172

173-
pub fn build_float_mul(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool) -> DiagnosticResult<MIRFloatValue> {
173+
pub fn build_float_mul(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool, fast: bool) -> DiagnosticResult<MIRFloatValue> {
174174
if left.size != right.size {
175175
unsure_panic!("Tried using fmul on different sized integers");
176176
}
177177

178-
let res = ctx.append_inst(MIRInstruction::FloatMul { signed, left, right }).get()?;
178+
let res = ctx.append_inst(MIRInstruction::FloatMul { signed, fast, left, right }).get()?;
179179

180180
return res.as_float();
181181
}
182182

183183

184-
pub fn build_float_div(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool) -> DiagnosticResult<MIRFloatValue> {
184+
pub fn build_float_div(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool, fast: bool) -> DiagnosticResult<MIRFloatValue> {
185185
if left.size != right.size {
186186
unsure_panic!("Tried using fdiv on different sized integers");
187187
}
188188

189-
let res = ctx.append_inst(MIRInstruction::FloatDiv { signed, left, right }).get()?;
189+
let res = ctx.append_inst(MIRInstruction::FloatDiv { signed, fast, left, right }).get()?;
190190

191191
return res.as_float();
192192
}
193193

194-
pub fn build_float_mod(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool) -> DiagnosticResult<MIRFloatValue> {
194+
pub fn build_float_mod(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool, fast: bool) -> DiagnosticResult<MIRFloatValue> {
195195
if left.size != right.size {
196196
unsure_panic!("Tried using fmod on different sized integers");
197197
}
198198

199-
let res = ctx.append_inst(MIRInstruction::FloatMod { signed, left, right }).get()?;
199+
let res = ctx.append_inst(MIRInstruction::FloatMod { signed, fast, left, right }).get()?;
200200

201201
return res.as_float();
202202
}

compiler/astoir_mir/src/insts/mod.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ pub enum MIRInstruction {
2323
UpcastFloat { val: MIRFloatValue, size: usize },
2424

2525
// Arithmetrics
26-
IntegerAdd { signed: bool, left: MIRIntValue, right: MIRIntValue },
27-
IntegerSub { signed: bool, left: MIRIntValue, right: MIRIntValue },
28-
IntegerMul { signed: bool, left: MIRIntValue, right: MIRIntValue },
29-
IntegerDiv { signed: bool, left: MIRIntValue, right: MIRIntValue },
30-
IntegerMod { signed: bool, left: MIRIntValue, right: MIRIntValue },
26+
IntegerAdd { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
27+
IntegerSub { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
28+
IntegerMul { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
29+
IntegerDiv { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
30+
IntegerMod { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
3131
IntegerNeg { val: MIRIntValue },
3232

33-
FloatAdd { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
34-
FloatSub { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
35-
FloatMul { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
36-
FloatDiv { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
37-
FloatMod { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
33+
FloatAdd { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
34+
FloatSub { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
35+
FloatMul { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
36+
FloatDiv { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
37+
FloatMod { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
3838
FloatNeg { val: MIRFloatValue },
3939

4040
// Bitwise (int typed)
@@ -139,17 +139,17 @@ impl MIRInstruction {
139139
Self::DowncastFloat { val, size } => return Type::GenericLowered(RawType::Floating(*size, val.signed)),
140140
Self::UpcastFloat { val, size } => return Type::GenericLowered(RawType::Floating(*size, val.signed)),
141141

142-
Self::IntegerAdd { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
143-
Self::IntegerSub { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
144-
Self::IntegerMul { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
145-
Self::IntegerDiv { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
146-
Self::IntegerMod { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
142+
Self::IntegerAdd { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
143+
Self::IntegerSub { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
144+
Self::IntegerMul { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
145+
Self::IntegerDiv { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
146+
Self::IntegerMod { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
147147
Self::IntegerNeg { val } => return Type::GenericLowered(RawType::Integer(val.size, true)),
148148

149-
Self::FloatAdd { signed, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
150-
Self::FloatSub { signed, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
151-
Self::FloatMul { signed, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
152-
Self::FloatDiv { signed, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
149+
Self::FloatAdd { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
150+
Self::FloatSub { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
151+
Self::FloatMul { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
152+
Self::FloatDiv { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
153153
Self::FloatNeg { val } => return Type::GenericLowered(RawType::Floating(val.size, true)),
154154

155155
Self::BitwiseAnd { a, b: _ } => return Type::GenericLowered(RawType::Integer(a.size, a.signed)),
@@ -220,18 +220,18 @@ impl Display for MIRInstruction {
220220
Self::UpcastInteger { val, size } => writeln!(f, "uintcast {} {}", val, size)?,
221221
Self::UpcastFloat { val, size } => writeln!(f, "ufcast {} {}", val, size)?,
222222

223-
Self::IntegerAdd { signed, left, right } => writeln!(f, "iadd s{} {} {}", signed, left, right)?,
224-
Self::IntegerSub { signed, left, right } => writeln!(f, "isub s{} {} {}", signed, left, right)?,
225-
Self::IntegerMul { signed, left, right } => writeln!(f, "imul s{} {} {}", signed, left, right)?,
226-
Self::IntegerDiv { signed, left, right } => writeln!(f, "idiv s{} {} {}", signed, left, right)?,
227-
Self::IntegerMod { signed, left, right } => writeln!(f, "imod s{} {} {}", signed, left, right)?,
223+
Self::IntegerAdd { signed, fast, left, right } => writeln!(f, "iadd s{} f{} {} {}", signed, fast, left, right)?,
224+
Self::IntegerSub { signed, fast, left, right } => writeln!(f, "isub s{} f{} {} {}", signed, fast, left, right)?,
225+
Self::IntegerMul { signed, fast, left, right } => writeln!(f, "imul s{} f{} {} {}", signed, fast, left, right)?,
226+
Self::IntegerDiv { signed, fast, left, right } => writeln!(f, "idiv s{} f{} {} {}", signed, fast, left, right)?,
227+
Self::IntegerMod { signed, fast, left, right } => writeln!(f, "imod s{} f{} {} {}", signed, fast, left, right)?,
228228
Self::IntegerNeg { val } => writeln!(f, "ineg {}", val)?,
229229

230-
Self::FloatAdd { signed, left, right } => writeln!(f, "fadd s{} {} {}", signed, left, right)?,
231-
Self::FloatSub { signed, left, right } => writeln!(f, "fsub s{} {} {}", signed, left, right)?,
232-
Self::FloatMul { signed, left, right } => writeln!(f, "fmul s{} {} {}", signed, left, right)?,
233-
Self::FloatDiv { signed, left, right } => writeln!(f, "fdiv s{} {} {}", signed, left, right)?,
234-
Self::FloatMod { signed, left, right } => writeln!(f, "fmod s{} {} {}", signed, left, right)?,
230+
Self::FloatAdd { signed, fast, left, right } => writeln!(f, "fadd s{} f{} {} {}", signed, fast, left, right)?,
231+
Self::FloatSub { signed, fast, left, right } => writeln!(f, "fsub s{} f{} {} {}", signed, fast, left, right)?,
232+
Self::FloatMul { signed, fast, left, right } => writeln!(f, "fmul s{} f{} {} {}", signed, fast, left, right)?,
233+
Self::FloatDiv { signed, fast, left, right } => writeln!(f, "fdiv s{} f{} {} {}", signed, fast, left, right)?,
234+
Self::FloatMod { signed, fast, left, right } => writeln!(f, "fmod s{} f{} {} {}", signed, fast, left, right)?,
235235
Self::FloatNeg { val } => writeln!(f, "fneg {}", val)?,
236236

237237
Self::BitwiseAnd { a, b } => writeln!(f, "and {} {}", a, b)?,

compiler/astoir_mir_lowering/src/math.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ pub fn lower_hir_math_operation_int(left: BaseMIRValue, right: BaseMIRValue, ope
5050
let signed = left.signed;
5151

5252
let res = match operator.operator {
53-
MathOperatorType::Add => build_int_add(&mut ctx.mir_ctx, left, right, signed)?,
54-
MathOperatorType::Subtract => build_int_sub(&mut ctx.mir_ctx, left, right, signed)?,
55-
MathOperatorType::Multiply => build_int_mul(&mut ctx.mir_ctx, left, right, signed)?,
56-
MathOperatorType::Divide => build_int_div(&mut ctx.mir_ctx, left, right, signed)?,
53+
MathOperatorType::Add => build_int_add(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
54+
MathOperatorType::Subtract => build_int_sub(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
55+
MathOperatorType::Multiply => build_int_mul(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
56+
MathOperatorType::Divide => build_int_div(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
5757
MathOperatorType::ShiftLeft => build_shift_left(&mut ctx.mir_ctx, left, right)?,
5858
MathOperatorType::ShiftRight => build_shift_right(&mut ctx.mir_ctx, left, right)?,
59-
MathOperatorType::Modulo => build_int_mod(&mut ctx.mir_ctx, left, right, signed)?
59+
MathOperatorType::Modulo => build_int_mod(&mut ctx.mir_ctx, left, right, signed, operator.fast)?
6060
};
6161

6262
return Ok(res.into());
@@ -69,11 +69,11 @@ pub fn lower_hir_math_operation_float(left: BaseMIRValue, right: BaseMIRValue, o
6969
let signed = left.signed;
7070

7171
let res = match operator.operator {
72-
MathOperatorType::Add => build_float_add(&mut ctx.mir_ctx, left, right, signed)?,
73-
MathOperatorType::Subtract => build_float_sub(&mut ctx.mir_ctx, left, right, signed)?,
74-
MathOperatorType::Multiply => build_float_mul(&mut ctx.mir_ctx, left, right, signed)?,
75-
MathOperatorType::Divide => build_float_div(&mut ctx.mir_ctx, left, right, signed)?,
76-
MathOperatorType::Modulo => build_float_mod(&mut ctx.mir_ctx, left, right, signed)?,
72+
MathOperatorType::Add => build_float_add(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
73+
MathOperatorType::Subtract => build_float_sub(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
74+
MathOperatorType::Multiply => build_float_mul(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
75+
MathOperatorType::Divide => build_float_div(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
76+
MathOperatorType::Modulo => build_float_mod(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
7777

7878
_ => return Err(make_req_type_kind(node, &"integer".to_string()).into())
7979
};

0 commit comments

Comments
 (0)