Skip to content

Commit 52d5bf4

Browse files
committed
feat(bleach): apply prefix to all lifted functions
1 parent 9d3f013 commit 52d5bf4

27 files changed

Lines changed: 126 additions & 133 deletions

lib/lifter/lifter.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -460,12 +460,10 @@ register_stats collect_register_stats(const instr_impl &instr, Module &m,
460460
throw std::runtime_error(
461461
"register-classes were not specified in input YAML");
462462
register_stats stats(rclasses.begin(), rclasses.end());
463-
const MCRegisterInfo *rinfo = nullptr;
464463
auto *stinfo = mmi.getTarget().getSubtargetImpl(*m.begin());
465464
for (auto &f : m) {
466465
auto &mf = mmi.getOrCreateMachineFunction(f);
467466
collect_register_stats_for(mf, mmi.getTarget(), stats);
468-
rinfo = stinfo->getRegisterInfo();
469467
}
470468
assert(!m.empty());
471469
assert(stinfo);
@@ -1180,24 +1178,8 @@ Module &bleach_module(Module &m, MachineModuleInfo &mmi,
11801178

11811179
create_bleach_symtab_add_function_decl(m);
11821180
create_bleach_symtab_lookup_function_decl(m);
1183-
// rename "main" function
1184-
auto *main_func = m.getFunction("main");
1185-
if (main_func)
1186-
main_func->setName(std::format("{}main", lifted_prefix));
1187-
// create state struct and generate header
1188-
auto &state = create_state_type(m.getContext(), reg_stats, stack_size);
1189-
if (!state_struct_file.empty()) {
1190-
if (state_struct_file == "-") {
1191-
print_state_header(std::cout, translated, state, reg_stats);
1192-
} else {
1193-
std::fstream fs(std::string(state_struct_file), std::fstream::out);
1194-
if (!fs.is_open())
1195-
throw std::runtime_error(
1196-
std::format("Could not open file \"{}\"", state_struct_file));
1197-
print_state_header(fs, translated, state, reg_stats);
1198-
}
1199-
}
12001181

1182+
auto &state = create_state_type(m.getContext(), reg_stats, stack_size);
12011183
for (auto &&[oldf, func_info] : funcs)
12021184
generate_function(*oldf, func_info, instrs, mmi, state, reg_stats,
12031185
assume_functions_nop);
@@ -1219,6 +1201,21 @@ Module &bleach_module(Module &m, MachineModuleInfo &mmi,
12191201
}
12201202
Linker::linkModules(m, std::move(extra));
12211203
}
1204+
// rename functions
1205+
for (auto *func : translated)
1206+
func->setName(std::format("{}{}", lifted_prefix, func->getName().data()));
1207+
// create state struct and generate header
1208+
if (!state_struct_file.empty()) {
1209+
if (state_struct_file == "-") {
1210+
print_state_header(std::cout, translated, state, reg_stats);
1211+
} else {
1212+
std::fstream fs(std::string(state_struct_file), std::fstream::out);
1213+
if (!fs.is_open())
1214+
throw std::runtime_error(
1215+
std::format("Could not open file \"{}\"", state_struct_file));
1216+
print_state_header(fs, translated, state, reg_stats);
1217+
}
1218+
}
12221219
for (auto *f : target_functions)
12231220
f->eraseFromParent();
12241221

test/integration/doubles-mixed.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
// RUN: %config-gen-path/config-gen.rb --march rv64imfd \
22
// RUN: --template-dir %config-gen-path/templates -o %t.yaml
3-
// RUN: riscv64-unknown-linux-gnu-clang %s -O2 -c -o %t.out -march=rv64imfd
3+
// RUN: riscv64-unknown-linux-gnu-clang %s -O2 -c -o %t.out -march=rv64imfd \
4+
// RUN: -I %S/inputs
45
// RUN: %bin/llvm-bleach %t.out --instructions %t.yaml -o %t.ll \
5-
// RUN: --state-struct-file=%t.state.h
6+
// RUN: --state-struct-file=%t.state.h --lifted-prefix=bleached_
67
// RUN: sed 's|STATE|%t.state.h|g' %S/inputs/doubles-mixed-main.c > %t.main.c
7-
// RUN: clang %t.main.c %t.ll -o %t.native.out -g -fsanitize=address,undefined
8+
// RUN: clang %t.main.c %t.ll -o %t.native.out -fsanitize=address,undefined \
9+
// RUN: -I %S/inputs
810
// RUN: %t.native.out
911

10-
double complex_mixed(double a, double b, double c, double d) {
11-
double part1 = (a + b) * (unsigned)(c - d);
12-
double part2 = (a * d) + (int)(b * c);
13-
double part3 = (a / c) * (long long)(b / d);
14-
return part1 + part2 - part3;
15-
}
12+
#include "doubles-mixed.h"

test/integration/indbr.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ main:
2828
addi a0, a0, -5
2929
ret
3030

31-
# CHECK: define void @count_to_5
32-
# CHECK-NEXT: call void @bleach_symtab_add(i64 0, ptr @count_to_5)
31+
# CHECK: define void @bleached_count_to_5
32+
# CHECK-NEXT: call void @bleach_symtab_add(i64 0, ptr @bleached_count_to_5)
3333
# CHECK: [[FUNCADDR:%[0-9]+]] = call ptr @bleach_symtab_lookup
3434
# CHECK: musttail call void [[FUNCADDR]]
3535

test/integration/inputs/doubles-mixed-main.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#include "doubles-mixed.h"
12
#include <STATE>
3+
24
#include <stdint.h>
35
#include <stdio.h>
46
#include <string.h>
@@ -17,15 +19,6 @@ double bitcast_to_double(int64_t v) {
1719
return res;
1820
}
1921

20-
// This should be in sync with test/integration/doubles-mixed.c
21-
// TODO: automate this
22-
double reference(double a, double b, double c, double d) {
23-
double part1 = (a + b) * (unsigned)(c - d);
24-
double part2 = (a * d) + (int)(b * c);
25-
double part3 = (a / c) * (long long)(b / d);
26-
return part1 + part2 - part3;
27-
}
28-
2922
int main() {
3023
printf("Hello from main\n");
3124
int arg_1 = 3;
@@ -36,8 +29,8 @@ int main() {
3629
regs.FPR[11] = bitcast_to_int(arg_2);
3730
regs.FPR[12] = bitcast_to_int(arg_3);
3831
regs.FPR[13] = bitcast_to_int(arg_4);
39-
complex_mixed(&regs);
40-
double res1 = reference(arg_1, arg_2, arg_3, arg_4);
32+
bleached_complex_mixed(&regs);
33+
double res1 = complex_mixed(arg_1, arg_2, arg_3, arg_4);
4134
double res2 = bitcast_to_double(regs.FPR[10]);
4235
printf("result: %lf\n", res2);
4336
printf("reference: %lf\n", res1);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
double complex_mixed(double a, double b, double c, double d) {
4+
double part1 = (a + b) * (unsigned)(c - d);
5+
double part2 = (a * d) + (int)(b * c);
6+
double part3 = (a / c) * (long long)(b / d);
7+
return part1 + part2 - part3;
8+
}

test/integration/inputs/riscv-mixed-fp.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include <STATE>
2+
#include <riscv-mixed-fp.h>
3+
24
#include <math.h>
35
#include <stdint.h>
46
#include <stdio.h>
@@ -18,18 +20,6 @@ double bitcast_to_double(int64_t v) {
1820
return res;
1921
}
2022

21-
// This should be in sync with test/integration/riscv-mixed-fp.c
22-
// TODO: automate this
23-
double ref_calc2(double prod, double sum, int x) {
24-
return prod + sum - (double)x;
25-
}
26-
27-
double ref_calc1(double a, double b, int x) {
28-
return ref_calc2(a * b, (a + b), x);
29-
}
30-
31-
double reference(double a, double b, int x) { return ref_calc1(a, b, x); }
32-
3323
int are_equal(double a, double b) {
3424
if (isnan(a) && isnan(b))
3525
return 1;
@@ -47,8 +37,8 @@ int main() {
4737
regs.FPR[10] = bitcast_to_int(arg_1);
4838
regs.FPR[11] = bitcast_to_int(arg_2);
4939
regs.GPR[10] = arg_3;
50-
top_small(&regs);
51-
double res1 = reference(arg_1, arg_2, arg_3);
40+
bleached_top_small(&regs);
41+
double res1 = top_small(arg_1, arg_2, arg_3);
5242
double res2 = bitcast_to_double(regs.FPR[10]);
5343
printf("result: %lf\n", res2);
5444
printf("reference: %lf\n", res1);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
double calc2(double prod, double sum, int x) { return prod + sum - (double)x; }
4+
5+
double calc1(double a, double b, int x) { return calc2(a * b, (a + b), x); }
6+
7+
double top_small(double a, double b, int x) { return calc1(a, b, x); }

test/integration/inputs/riscv-mixed-precision-fp.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#include "riscv-mixed-precision-fp.h"
12
#include <STATE>
3+
24
#include <math.h>
35
#include <stdint.h>
46
#include <stdio.h>
@@ -18,18 +20,6 @@ double bitcast_to_double(int64_t v) {
1820
return res;
1921
}
2022

21-
// This should be in sync with test/integration/riscv-mixed-fp.c
22-
// TODO: automate this
23-
double ref_calc2(float prod, double sum, int x) {
24-
return prod + sum - (double)x;
25-
}
26-
27-
double ref_calc1(float a, float b, int x) {
28-
return ref_calc2(a * b, (a + b), x + a / b);
29-
}
30-
31-
double reference(double a, double b, int x) { return ref_calc1(a, b, x); }
32-
3323
int are_equal(double a, double b) {
3424
if (isnan(a) && isnan(b))
3525
return 1;
@@ -47,8 +37,8 @@ int main() {
4737
regs.FPR[10] = bitcast_to_int(arg_1);
4838
regs.FPR[11] = bitcast_to_int(arg_2);
4939
regs.GPR[10] = arg_3;
50-
top_small(&regs);
51-
double res1 = reference(arg_1, arg_2, arg_3);
40+
bleached_top_small(&regs);
41+
double res1 = top_small(arg_1, arg_2, arg_3);
5242
double res2 = bitcast_to_double(regs.FPR[10]);
5343
printf("result: %lf\n", res2);
5444
printf("reference: %lf\n", res1);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
double calc2(float prod, double sum, int x) { return prod + sum - (double)x; }
4+
5+
double calc1(float a, float b, int x) {
6+
return calc2(a * b, (a + b), x + a / b);
7+
}
8+
9+
double top_small(double a, double b, int x) { return calc1(a, b, x); }

test/integration/riscv-mixed-fp.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// RUN: %config-gen-path/config-gen.rb --march rv64imfd \
22
// RUN: --template-dir %config-gen-path/templates -o %t.yaml
3-
// RUN: riscv64-unknown-linux-gnu-clang %s -O2 -c -o %t.out -march=rv64imfd
3+
// RUN: riscv64-unknown-linux-gnu-clang %s -O2 -c -o %t.out -march=rv64imfd \
4+
// RUN: -I %S/inputs
45
// RUN: %bin/llvm-bleach %t.out --instructions %t.yaml -o %t.ll \
5-
// RUN: --state-struct-file=%t.state.h
6+
// RUN: --state-struct-file=%t.state.h --lifted-prefix=bleached_
67
// RUN: sed 's|STATE|%t.state.h|g' %S/inputs/riscv-mixed-fp.c > %t.main.c
7-
// RUN: clang %t.main.c %t.ll -o %t.native.out -g -fsanitize=address,undefined
8+
// RUN: clang %t.main.c %t.ll -o %t.native.out -g -fsanitize=address,undefined \
9+
// RUN: -I %S/inputs
810
// RUN: %t.native.out
911

10-
double calc2(double prod, double sum, int x) { return prod + sum - (double)x; }
11-
12-
double calc1(double a, double b, int x) { return calc2(a * b, (a + b), x); }
13-
14-
double top_small(double a, double b, int x) { return calc1(a, b, x); }
12+
#include "riscv-mixed-fp.h"

0 commit comments

Comments
 (0)