Skip to content

Commit 90127fa

Browse files
committed
move pre-normalized IR passes in the frontends
1 parent 25ba91a commit 90127fa

File tree

21 files changed

+167
-59
lines changed

21 files changed

+167
-59
lines changed

include/shady/driver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ typedef enum {
2626
} SourceLanguage;
2727

2828
SourceLanguage guess_source_language(const char* filename);
29-
ShadyErrorCodes driver_load_source_file(const CompilerConfig* config, SourceLanguage lang, size_t, const char* file_contents, Module* mod);
30-
ShadyErrorCodes driver_load_source_file_from_filename(const CompilerConfig* config, const char* filename, Module* mod);
29+
ShadyErrorCodes driver_load_source_file(const CompilerConfig* config, SourceLanguage lang, size_t, const char* file_contents, String, Module** mod);
30+
ShadyErrorCodes driver_load_source_file_from_filename(const CompilerConfig* config, const char* filename, String, Module** mod);
3131

3232
typedef enum {
3333
TgtAuto,

include/shady/ir.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ typedef enum CompilationResult_ {
326326
} CompilationResult;
327327

328328
CompilationResult run_compiler_passes(CompilerConfig* config, Module** mod);
329+
void link_module(Module* dst, Module* src);
329330

330331
//////////////////////////////// Emission ////////////////////////////////
331332

include/shady/runtime.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ typedef struct CompilerConfig_ CompilerConfig;
2929
typedef struct Module_ Module;
3030

3131
Program* new_program_from_module(Runtime*, const CompilerConfig*, Module*);
32-
Program* load_program(Runtime*, const CompilerConfig*, const char* program_src);
33-
Program* load_program_from_disk(Runtime*, const CompilerConfig*, const char* path);
3432

3533
Command* launch_kernel(Program*, Device*, const char* entry_point, int dimx, int dimy, int dimz, int args_count, void** args);
3634
bool wait_completion(Command*);

samples/aobench/ao_main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ void render_device(Args* args, TEXEL_T *img, int w, int h, int nsubsamples, Stri
127127

128128
info_print("Device-side address is: %zu\n", buf_addr);
129129

130-
Program* program = load_program_from_disk(runtime, &args->compiler_config, path);
130+
Module* m;
131+
CHECK(driver_load_source_file_from_filename(&args->compiler_config, path, "aobench", &m) == NoError, return);
132+
Program* program = new_program_from_module(runtime, &args->compiler_config, m);
131133

132134
// run it twice to compile everything and benefit from caches
133135
wait_completion(launch_kernel(program, device, "aobench_kernel", WIDTH / BLOCK_SIZE, HEIGHT / BLOCK_SIZE, 1, 1, (void*[]) { &buf_addr }));

samples/checkerboard/checkerboard.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ int main(int argc, char **argv)
6868
info_print("Device-side address is: %zu\n", buf_addr);
6969

7070
IrArena* a = new_ir_arena(default_arena_config());
71-
Module* m = new_module(a, "checkerboard");
72-
driver_load_source_file(&compiler_config, SrcSlim, sizeof(checkerboard_kernel_src), checkerboard_kernel_src, m);
71+
Module* m;
72+
if (driver_load_source_file(&compiler_config, SrcSlim, sizeof(checkerboard_kernel_src), checkerboard_kernel_src, "checkerboard", &m) != NoError)
73+
error("Failed to load checkerboard module");
7374
Program* program = new_program_from_module(runtime, &compiler_config, m);
7475

7576
wait_completion(launch_kernel(program, device, "main", 16, 16, 1, 1, (void*[]) { &buf_addr }));

src/common/log.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void set_log_level(LogLevel);
2020
void log_string(LogLevel level, const char* format, ...);
2121
void log_node(LogLevel level, const Node* node);
2222
typedef struct CompilerConfig_ CompilerConfig;
23-
void log_module(LogLevel level, CompilerConfig*, Module*);
23+
void log_module(LogLevel level, const CompilerConfig*, Module*);
2424

2525
#define debugvv_print(...) log_string(DEBUGVV, __VA_ARGS__)
2626
#define debugv_print(...) log_string(DEBUGV, __VA_ARGS__)

src/driver/driver.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,20 @@ SourceLanguage guess_source_language(const char* filename) {
3737
return SrcSlim;
3838
}
3939

40-
ShadyErrorCodes driver_load_source_file(const CompilerConfig* config, SourceLanguage lang, size_t len, const char* file_contents, Module* mod) {
40+
ShadyErrorCodes driver_load_source_file(const CompilerConfig* config, SourceLanguage lang, size_t len, const char* file_contents, String name, Module** mod) {
4141
switch (lang) {
4242
case SrcLLVM: {
4343
#ifdef LLVM_PARSER_PRESENT
44-
parse_llvm_into_shady(config, mod, len, file_contents);
44+
bool ok = parse_llvm_into_shady(config, len, file_contents, name, mod);
45+
assert(ok);
4546
#else
4647
assert(false && "LLVM front-end missing in this version");
4748
#endif
4849
break;
4950
}
5051
case SrcSPIRV: {
5152
#ifdef SPV_PARSER_PRESENT
52-
parse_spirv_into_shady(mod, len, file_contents);
53+
parse_spirv_into_shady(len, file_contents, name, mod);
5354
#else
5455
assert(false && "SPIR-V front-end missing in this version");
5556
#endif
@@ -58,16 +59,16 @@ ShadyErrorCodes driver_load_source_file(const CompilerConfig* config, SourceLang
5859
case SrcShadyIR:
5960
case SrcSlim: {
6061
ParserConfig pconfig = {
61-
.front_end = lang == SrcSlim,
62+
.front_end = lang == SrcSlim,
6263
};
6364
debugv_print("Parsing: \n%s\n", file_contents);
64-
parse_shady_ir(pconfig, (const char*) file_contents, mod);
65+
*mod = parse_slim_module(config, pconfig, (const char*) file_contents, name);
6566
}
6667
}
6768
return NoError;
6869
}
6970

70-
ShadyErrorCodes driver_load_source_file_from_filename(const CompilerConfig* config, const char* filename, Module* mod) {
71+
ShadyErrorCodes driver_load_source_file_from_filename(const CompilerConfig* config, const char* filename, String name, Module** mod) {
7172
ShadyErrorCodes err;
7273
SourceLanguage lang = guess_source_language(filename);
7374
size_t len;
@@ -84,7 +85,7 @@ ShadyErrorCodes driver_load_source_file_from_filename(const CompilerConfig* conf
8485
err = InputFileDoesNotExist;
8586
goto exit;
8687
}
87-
err = driver_load_source_file(config, lang, len, contents, mod);
88+
err = driver_load_source_file(config, lang, len, contents, name, mod);
8889
exit:
8990
free((void*) contents);
9091
return err;
@@ -98,9 +99,11 @@ ShadyErrorCodes driver_load_source_files(DriverConfig* args, Module* mod) {
9899

99100
size_t num_source_files = entries_count_list(args->input_filenames);
100101
for (size_t i = 0; i < num_source_files; i++) {
101-
int err = driver_load_source_file_from_filename(&args->config, read_list(const char*, args->input_filenames)[i], mod);
102+
Module* m;
103+
int err = driver_load_source_file_from_filename(&args->config, read_list(const char*, args->input_filenames)[i], read_list(const char*, args->input_filenames)[i], &m);
102104
if (err)
103105
return err;
106+
link_module(mod, m);
104107
}
105108

106109
return NoError;

src/driver/vcc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ int main(int argc, char** argv) {
7070

7171
ArenaConfig aconfig = default_arena_config();
7272
IrArena* arena = new_ir_arena(aconfig);
73-
Module* mod = new_module(arena, "my_module"); // TODO name module after first filename, or perhaps the last one
7473

7574
int clang_retval = system("clang --version");
7675
if (clang_retval != 0)
@@ -140,12 +139,13 @@ int main(int argc, char** argv) {
140139
if (clang_returned)
141140
exit(ClangInvocationFailed);
142141

142+
Module* mod;
143143
if (!vcc_options.only_run_clang) {
144144
size_t len;
145145
char* llvm_ir;
146146
if (!read_file(vcc_options.tmp_filename, &len, &llvm_ir))
147147
exit(InputFileIOError);
148-
driver_load_source_file(&args.config, SrcLLVM, len, llvm_ir, mod);
148+
driver_load_source_file(&args.config, SrcLLVM, len, llvm_ir, "my_module", &mod); // TODO name module after first filename, or perhaps the last one
149149
free(llvm_ir);
150150

151151
if (vcc_options.delete_tmp_file)

src/frontends/llvm/l2s.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ const Node* convert_global(Parser* p, LLVMValueRef global) {
230230
return r;
231231
}
232232

233-
bool parse_llvm_into_shady(const CompilerConfig* config, Module* dst, size_t len, const char* data) {
233+
bool parse_llvm_into_shady(const CompilerConfig* config, size_t len, const char* data, String name, Module** dst) {
234234
LLVMContextRef context = LLVMContextCreate();
235235
LLVMModuleRef src;
236236
LLVMMemoryBufferRef mem = LLVMCreateMemoryBufferWithMemoryRange(data, len, "my_great_buffer", false);
@@ -245,7 +245,12 @@ bool parse_llvm_into_shady(const CompilerConfig* config, Module* dst, size_t len
245245
get_module_arena(dst)->config.untyped_ptrs = true; // tolerate untyped ptrs...
246246
#endif
247247

248-
Module* dirty = new_module(get_module_arena(dst), "dirty");
248+
ArenaConfig aconfig = default_arena_config();
249+
aconfig.check_types = false;
250+
aconfig.allow_fold = false;
251+
252+
IrArena* arena = new_ir_arena(aconfig);
253+
Module* dirty = new_module(arena, "dirty");
249254
Parser p = {
250255
.ctx = context,
251256
.config = config,
@@ -274,7 +279,14 @@ bool parse_llvm_into_shady(const CompilerConfig* config, Module* dst, size_t len
274279
global = LLVMGetNextGlobal(global);
275280
}
276281

277-
postprocess(&p, dirty, dst);
282+
aconfig.check_types = true;
283+
aconfig.allow_fold = true;
284+
IrArena* arena2 = new_ir_arena(aconfig);
285+
*dst = new_module(arena2, name);
286+
postprocess(&p, dirty, *dst);
287+
log_module(DEBUG, config, *dst);
288+
verify_module(config, *dst);
289+
destroy_ir_arena(arena);
278290

279291
destroy_dict(p.map);
280292
destroy_dict(p.annotations);

src/frontends/llvm/l2s.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
#include "shady/ir.h"
55
#include <stdbool.h>
66

7-
bool parse_llvm_into_shady(const CompilerConfig*, Module* dst, size_t len, const char* data);
7+
bool parse_llvm_into_shady(const CompilerConfig*, size_t len, const char* data, String name, Module** dst);
88

99
#endif

0 commit comments

Comments
 (0)