Skip to content

Commit 6699d9b

Browse files
committed
added lower_nullptr pass
1 parent d6139bf commit 6699d9b

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

src/shady/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ set(SHADY_SOURCES
7070
passes/lower_tailcalls.c
7171
passes/lower_mask.c
7272
passes/lower_fill.c
73+
passes/lower_nullptr.c
7374
passes/lower_switch_btree.c
7475
passes/setup_stack_frames.c
7576
passes/eliminate_constants.c

src/shady/compile.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ CompilationResult run_compiler_passes(CompilerConfig* config, Module** pmod) {
151151
if (config->lower.simt_to_explicit_simd)
152152
RUN_PASS(simt2d)
153153
RUN_PASS(lower_fill)
154+
RUN_PASS(lower_nullptr)
154155
RUN_PASS(normalize_builtins)
155156

156157
return CompilationNoError;

src/shady/passes/lower_nullptr.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "passes.h"
2+
3+
#include "log.h"
4+
#include "portability.h"
5+
#include "dict.h"
6+
7+
#include "../ir_private.h"
8+
#include "../type.h"
9+
#include "../rewrite.h"
10+
#include "../transform/ir_gen_helpers.h"
11+
12+
typedef struct {
13+
Rewriter rewriter;
14+
struct Dict* map;
15+
} Context;
16+
17+
static const Node* make_nullptr(Context* ctx, const Type* t) {
18+
IrArena* a = ctx->rewriter.dst_arena;
19+
const Node** found = find_value_dict(const Type*, const Node*, ctx->map, t);
20+
if (found)
21+
return *found;
22+
23+
BodyBuilder* bb = begin_body(a);
24+
const Node* nul = gen_reinterpret_cast(bb, t, uint64_literal(a, 0));
25+
Node* decl = constant(ctx->rewriter.dst_module, singleton(annotation(a, (Annotation) {
26+
.name = "Generated",
27+
})), t, format_string_interned(a, "nullptr_%s", name_type_safe(a, t)));
28+
decl->payload.constant.instruction = yield_values_and_wrap_in_block(bb, singleton(nul));
29+
const Node* ref = ref_decl_helper(a, decl);
30+
insert_dict(const Type*, const Node*, ctx->map, t, ref);
31+
return ref;
32+
}
33+
34+
static const Node* process(Context* ctx, const Node* node) {
35+
IrArena* a = ctx->rewriter.dst_arena;
36+
Rewriter* r = &ctx->rewriter;
37+
switch (node->tag) {
38+
case NullPtr_TAG: {
39+
const Type* t = rewrite_node(r, node->payload.null_ptr.ptr_type);
40+
assert(t->tag == PtrType_TAG);
41+
return make_nullptr(ctx, t);
42+
}
43+
default: break;
44+
}
45+
46+
return recreate_node_identity(&ctx->rewriter, node);
47+
}
48+
49+
KeyHash hash_node(Node**);
50+
bool compare_node(Node**, Node**);
51+
52+
Module* lower_nullptr(SHADY_UNUSED const CompilerConfig* config, Module* src) {
53+
ArenaConfig aconfig = get_arena_config(get_module_arena(src));
54+
IrArena* a = new_ir_arena(aconfig);
55+
Module* dst = new_module(a, get_module_name(src));
56+
Context ctx = {
57+
.rewriter = create_rewriter(src, dst, (RewriteNodeFn) process),
58+
.map = new_dict(const Node*, Node*, (HashFn) hash_node, (CmpFn) compare_node),
59+
};
60+
rewrite_module(&ctx.rewriter);
61+
destroy_rewriter(&ctx.rewriter);
62+
destroy_dict(ctx.map);
63+
return dst;
64+
}

src/shady/passes/passes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ RewritePass lower_int;
9898
RewritePass lower_vec_arr;
9999
RewritePass lower_workgroups;
100100
RewritePass lower_fill;
101+
RewritePass lower_nullptr;
101102

102103
/// @}
103104

0 commit comments

Comments
 (0)