Skip to content

Commit 438c2c3

Browse files
authored
add llvm.assume after bounds checks, full fast-math flags (nnan ninf) on all fp ops (#492)
Co-authored-by: cs01 <cs01@users.noreply.github.com>
1 parent 65067b6 commit 438c2c3

4 files changed

Lines changed: 17 additions & 11 deletions

File tree

src/codegen/expressions/access/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ export class IndexAccessGenerator {
201201
this.ctx.emit(`call void @__cs_bounds_fail(i32 ${index}, i32 ${len})`);
202202
this.ctx.emit(`unreachable`);
203203
this.ctx.emit(`${okLabel}:`);
204+
const inBounds = this.ctx.nextTemp();
205+
this.ctx.emit(`${inBounds} = icmp ult i32 ${index}, ${len}`);
206+
this.ctx.emit(`call void @llvm.assume(i1 ${inBounds})`);
204207
}
205208

206209
private generateStringArrayIndex(expr: IndexAccessNode, params: string[]): string {

src/codegen/expressions/operators/binary.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ export class BinaryExpressionGenerator {
8888
const leftValue = this.ctx.generateExpression(left, params);
8989
const rightValue = this.ctx.generateExpression(right, params);
9090

91-
// Arithmetic operators (floating-point)
91+
// Arithmetic operators (floating-point) — fast-math flags enable LLVM optimizations
9292
const arithMap: { [key: string]: string } = {
93-
"+": "fadd nsz arcp contract reassoc afn",
94-
"-": "fsub nsz arcp contract reassoc afn",
95-
"*": "fmul nsz arcp contract reassoc afn",
96-
"/": "fdiv nsz arcp contract reassoc afn",
93+
"+": "fadd nnan ninf nsz arcp contract reassoc afn",
94+
"-": "fsub nnan ninf nsz arcp contract reassoc afn",
95+
"*": "fmul nnan ninf nsz arcp contract reassoc afn",
96+
"/": "fdiv nnan ninf nsz arcp contract reassoc afn",
9797
};
9898

9999
// Bitwise operators (need to convert double -> i64 -> operate -> double)

src/codegen/infrastructure/ir-builders.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,32 @@ export function emitMul(ctx: EmitContext, type: string, lhs: string, rhs: string
2525
return temp;
2626
}
2727

28+
const FMATH = "nnan ninf nsz arcp contract reassoc afn";
29+
2830
export function emitFAdd(ctx: EmitContext, lhs: string, rhs: string): string {
2931
const temp = ctx.nextTemp();
30-
ctx.emit(`${temp} = fadd double ${lhs}, ${rhs}`);
32+
ctx.emit(`${temp} = fadd ${FMATH} double ${lhs}, ${rhs}`);
3133
ctx.setVariableType(temp, "double");
3234
return temp;
3335
}
3436

3537
export function emitFSub(ctx: EmitContext, lhs: string, rhs: string): string {
3638
const temp = ctx.nextTemp();
37-
ctx.emit(`${temp} = fsub double ${lhs}, ${rhs}`);
39+
ctx.emit(`${temp} = fsub ${FMATH} double ${lhs}, ${rhs}`);
3840
ctx.setVariableType(temp, "double");
3941
return temp;
4042
}
4143

4244
export function emitFMul(ctx: EmitContext, lhs: string, rhs: string): string {
4345
const temp = ctx.nextTemp();
44-
ctx.emit(`${temp} = fmul double ${lhs}, ${rhs}`);
46+
ctx.emit(`${temp} = fmul ${FMATH} double ${lhs}, ${rhs}`);
4547
ctx.setVariableType(temp, "double");
4648
return temp;
4749
}
4850

4951
export function emitFDiv(ctx: EmitContext, lhs: string, rhs: string): string {
5052
const temp = ctx.nextTemp();
51-
ctx.emit(`${temp} = fdiv double ${lhs}, ${rhs}`);
53+
ctx.emit(`${temp} = fdiv ${FMATH} double ${lhs}, ${rhs}`);
5254
ctx.setVariableType(temp, "double");
5355
return temp;
5456
}
@@ -62,14 +64,14 @@ export function emitSRem(ctx: EmitContext, type: string, lhs: string, rhs: strin
6264

6365
export function emitFRem(ctx: EmitContext, lhs: string, rhs: string): string {
6466
const temp = ctx.nextTemp();
65-
ctx.emit(`${temp} = frem double ${lhs}, ${rhs}`);
67+
ctx.emit(`${temp} = frem ${FMATH} double ${lhs}, ${rhs}`);
6668
ctx.setVariableType(temp, "double");
6769
return temp;
6870
}
6971

7072
export function emitFNeg(ctx: EmitContext, value: string): string {
7173
const temp = ctx.nextTemp();
72-
ctx.emit(`${temp} = fneg double ${value}`);
74+
ctx.emit(`${temp} = fneg ${FMATH} double ${value}`);
7375
ctx.setVariableType(temp, "double");
7476
return temp;
7577
}

src/codegen/infrastructure/llvm-declarations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export function getLLVMDeclarations(config?: DeclConfig): string {
5151
ir += "declare i8* @strstr(i8*, i8*)\n";
5252
ir += "declare i8* @strchr(i8*, i32)\n";
5353
ir += "declare i8* @strrchr(i8*, i32)\n";
54+
ir += "declare void @llvm.assume(i1)\n";
5455
ir += "declare void @llvm.memcpy.p0i8.p0i8.i64(i8*, i8*, i64, i1)\n";
5556
ir += "declare void @llvm.memmove.p0i8.p0i8.i64(i8*, i8*, i64, i1)\n";
5657
ir += "declare void @llvm.memset.p0i8.i64(i8*, i8, i64, i1)\n";

0 commit comments

Comments
 (0)