Skip to content

Commit 4d4dd59

Browse files
committed
introduce Params
1 parent c963da4 commit 4d4dd59

39 files changed

+245
-242
lines changed

include/shady/grammar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ inline static bool is_nominal(const Node* node) {
8585
NodeTag tag = node->tag;
8686
if (node->tag == PrimOp_TAG && has_primop_got_side_effects(node->payload.prim_op.op))
8787
return true;
88-
return tag == Function_TAG || tag == BasicBlock_TAG || tag == Constant_TAG || tag == Variable_TAG || tag == GlobalVariable_TAG || tag == NominalType_TAG || tag == Case_TAG;
88+
return tag == Function_TAG || tag == BasicBlock_TAG || tag == Constant_TAG || tag == Param_TAG || tag == GlobalVariable_TAG || tag == NominalType_TAG || tag == Case_TAG;
8989
}
9090

9191
inline static bool is_function(const Node* node) { return node->tag == Function_TAG; }

include/shady/grammar.json

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"name": "value"
8686
},
8787
{
88-
"name": "variable",
88+
"name": "param",
8989
"generate-enum": false
9090
},
9191
{
@@ -241,9 +241,8 @@
241241
]
242242
},
243243
{
244-
"name": "Variable",
245-
"snake_name": "var",
246-
"class": ["value", "variable"],
244+
"name": "Param",
245+
"class": ["value", "param"],
247246
"constructor": "custom",
248247
"ops": [
249248
{ "name": "type", "class": "type" },
@@ -252,6 +251,17 @@
252251
{ "name": "pindex", "type": "unsigned", "ignore": true }
253252
]
254253
},
254+
{
255+
"name": "Variablez",
256+
"snake_name": "varz",
257+
"class": ["value"],
258+
"constructor": "custom",
259+
"ops": [
260+
{ "name": "name", "class": "string" },
261+
{ "name": "instruction", "class": "instruction" },
262+
{ "name": "iindex", "type": "unsigned" }
263+
]
264+
},
255265
{
256266
"name": "ConstrainedValue",
257267
"snake_name": "constrained",
@@ -448,7 +458,7 @@
448458
"front-end-only": true,
449459
"ops": [
450460
{ "name": "instruction", "class": "instruction" },
451-
{ "name": "variables", "class": "variable", "list": true }
461+
{ "name": "variables", "class": "param", "list": true }
452462
]
453463
},
454464
{
@@ -548,7 +558,7 @@
548558
{ "name": "module", "type": "Module*", "ignore": true },
549559
{ "name": "name", "class": "string" },
550560
{ "name": "annotations", "class": "annotation", "list": true },
551-
{ "name": "params", "class": "variable", "list": true },
561+
{ "name": "params", "class": "param", "list": true },
552562
{ "name": "body", "class": "terminator" },
553563
{ "name": "return_types", "class": "type", "list": true }
554564
]
@@ -633,7 +643,7 @@
633643
"description": "A named abstraction that lives inside a function and can be jumped to",
634644
"class": "basic_block",
635645
"ops": [
636-
{ "name": "params", "class": "variable", "list": true },
646+
{ "name": "params", "class": "param", "list": true },
637647
{ "name": "body", "class": "terminator" },
638648
{ "name": "fn", "class": "declaration" },
639649
{ "name": "name", "class": "string" }
@@ -649,7 +659,7 @@
649659
"Most notably, the tails of standard `let` nodes"
650660
],
651661
"ops": [
652-
{ "name": "params", "class": "variable", "list": true },
662+
{ "name": "params", "class": "param", "list": true },
653663
{ "name": "body", "class": "terminator" },
654664
{ "name": "structured_construct", "type": "const Node*", "ignored": true }
655665
]

include/shady/ir.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ const Node* get_node_by_id(const IrArena*, NodeId);
120120
//////////////////////////////// Getters ////////////////////////////////
121121

122122
/// Get the name out of a global variable, function or constant
123-
String get_value_name(const Node*);
124123
String get_value_name_safe(const Node*);
124+
String get_value_name_unsafe(const Node*);
125125
void set_variable_name(Node* var, String);
126126

127127
const Node* get_quoted_value(const Node* instruction);
@@ -166,7 +166,7 @@ NodeResolveConfig default_node_resolve_config();
166166
const Node* resolve_ptr_to_value(const Node* node, NodeResolveConfig config);
167167

168168
/// Resolves a variable to the instruction that produces its value (if any)
169-
const Node* get_var_def(Variable var);
169+
const Node* get_var_def(Variablez var);
170170
const Node* resolve_node_to_definition(const Node* node, NodeResolveConfig config);
171171

172172
//////////////////////////////// Constructors ////////////////////////////////
@@ -209,7 +209,7 @@ const Node* fp_literal_helper(IrArena*, FloatSizes, double);
209209
const Node* type_decl_ref_helper(IrArena*, const Node* decl);
210210

211211
// values
212-
Node* var(IrArena*, const Type* type, const char* name);
212+
Node* param(IrArena*, const Type* type, const char* name);
213213

214214
const Node* tuple_helper(IrArena*, Nodes contents);
215215
const Node* composite_helper(IrArena*, const Type*, Nodes contents);

src/frontends/llvm/l2s_instr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,9 @@ EmittedInstr convert_instruction(Parser* p, FnParseCtx* fn_ctx, Node* fn_or_bb,
487487
assert(LLVMIsAFunction(callee));
488488
if (strcmp(intrinsic, "llvm.dbg.declare") == 0) {
489489
const Node* target = convert_value(p, LLVMGetOperand(instr, 0));
490-
if (target->tag != Variable_TAG)
490+
if (target->tag != Variablez_TAG)
491491
return (EmittedInstr) { 0 };
492-
assert(target->tag == Variable_TAG);
492+
assert(target->tag == Variablez_TAG);
493493
const Node* meta = convert_value(p, LLVMGetOperand(instr, 1));
494494
assert(meta->tag == RefDecl_TAG);
495495
meta = meta->payload.ref_decl.decl;

src/frontends/llvm/l2s_postprocess.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ static const Node* wrap_in_controls(Context* ctx, Controls* controls, const Node
4141
Nodes o_dst_params = get_abstraction_params(dst);
4242
LARRAY(const Node*, new_control_params, o_dst_params.count);
4343
for (size_t j = 0; j < o_dst_params.count; j++)
44-
new_control_params[j] = var(a, o_dst_params.nodes[j]->payload.var.type, unique_name(a, "v"));
44+
new_control_params[j] = param(a, o_dst_params.nodes[j]->payload.param.type, unique_name(a, "v"));
4545
Nodes nparams = nodes(a, o_dst_params.count, new_control_params);
4646
body = let(a, control(a, (Control) {
47-
.yield_types = get_variables_types(a, o_dst_params),
47+
.yield_types = get_param_types(a, o_dst_params),
4848
.inside = case_(a, singleton(token), body)
4949
}), case_(a, nparams, jump_helper(a, rewrite_node(&ctx->rewriter, dst), nparams)));
5050
}
@@ -66,13 +66,13 @@ bool lexical_scope_is_nested(Nodes scope, Nodes parentMaybe) {
6666

6767
bool compare_nodes(Nodes* a, Nodes* b);
6868

69-
static Nodes remake_variables(Context* ctx, Nodes old) {
69+
static Nodes remake_params(Context* ctx, Nodes old) {
7070
IrArena* a = ctx->rewriter.dst_arena;
7171
LARRAY(const Node*, nvars, old.count);
7272
for (size_t i = 0; i < old.count; i++) {
7373
const Node* node = old.nodes[i];
74-
nvars[i] = var(a, node->payload.var.type ? qualified_type_helper(rewrite_node(&ctx->rewriter, node->payload.var.type), false) : NULL, node->payload.var.name);
75-
assert(nvars[i]->tag == Variable_TAG);
74+
nvars[i] = param(a, node->payload.param.type ? qualified_type_helper(rewrite_node(&ctx->rewriter, node->payload.param.type), false) : NULL, node->payload.param.name);
75+
assert(nvars[i]->tag == Param_TAG);
7676
}
7777
return nodes(a, old.count, nvars);
7878
}
@@ -81,11 +81,11 @@ static const Node* process_op(Context* ctx, NodeClass op_class, String op_name,
8181
IrArena* a = ctx->rewriter.dst_arena;
8282
Rewriter* r = &ctx->rewriter;
8383
switch (node->tag) {
84-
case Variable_TAG: {
85-
assert(node->payload.var.type);
86-
if (node->payload.var.type->tag == QualifiedType_TAG)
87-
return var(a, node->payload.var.type ? rewrite_node(&ctx->rewriter, node->payload.var.type) : NULL, node->payload.var.name);
88-
return var(a, qualified_type_helper(rewrite_node(&ctx->rewriter, node->payload.var.type), false), node->payload.var.name);
84+
case Param_TAG: {
85+
assert(node->payload.param.type);
86+
if (node->payload.param.type->tag == QualifiedType_TAG)
87+
return param(a, node->payload.param.type ? rewrite_node(&ctx->rewriter, node->payload.param.type) : NULL, node->payload.param.name);
88+
return param(a, qualified_type_helper(rewrite_node(&ctx->rewriter, node->payload.param.type), false), node->payload.param.name);
8989
}
9090
case Block_TAG: {
9191
Nodes yield_types = rewrite_nodes(r, node->payload.block.yield_types);
@@ -131,7 +131,7 @@ static const Node* process_op(Context* ctx, NodeClass op_class, String op_name,
131131
fn_ctx.old_fn_or_bb = node;
132132
Controls controls;
133133
initialize_controls(ctx, &controls, node);
134-
Nodes new_params = recreate_variables(&fn_ctx.rewriter, node->payload.fun.params);
134+
Nodes new_params = recreate_params(&fn_ctx.rewriter, node->payload.fun.params);
135135
Nodes old_annotations = node->payload.fun.annotations;
136136
ParsedAnnotation* an = find_annotation(ctx->p, node);
137137
Op primop_intrinsic = PRIMOPS_COUNT;
@@ -150,7 +150,7 @@ static const Node* process_op(Context* ctx, NodeClass op_class, String op_name,
150150
primop_intrinsic = op;
151151
} else if (strcmp(get_annotation_name(an->payload), "EntryPoint") == 0) {
152152
for (size_t i = 0; i < new_params.count; i++)
153-
new_params = change_node_at_index(a, new_params, i, var(a, qualified_type_helper(get_unqualified_type(new_params.nodes[i]->payload.var.type), true), new_params.nodes[i]->payload.var.name));
153+
new_params = change_node_at_index(a, new_params, i, param(a, qualified_type_helper(get_unqualified_type(new_params.nodes[i]->payload.param.type), true), new_params.nodes[i]->payload.param.name));
154154
}
155155
old_annotations = append_nodes(a, old_annotations, an->payload);
156156
an = an->next;
@@ -225,13 +225,13 @@ static const Node* process_op(Context* ctx, NodeClass op_class, String op_name,
225225
}
226226
if (!join_token) {
227227
const Type* jp_type = join_point_type(a, (JoinPointType) {
228-
.yield_types = get_variables_types(a, get_abstraction_params(dst))
228+
.yield_types = get_param_types(a, get_abstraction_params(dst))
229229
});
230-
join_token = var(a, qualified_type_helper(jp_type, false), get_abstraction_name(dst));
230+
join_token = param(a, qualified_type_helper(jp_type, false), get_abstraction_name(dst));
231231
controls->tokens = append_nodes(a, controls->tokens, join_token);
232232
controls->destinations = append_nodes(a, controls->destinations, dst);
233233
}
234-
Nodes nparams = remake_variables(ctx, get_abstraction_params(dst));
234+
Nodes nparams = remake_params(ctx, get_abstraction_params(dst));
235235
//register_processed_list(&ctx->rewriter, get_abstraction_params(dst), nparams);
236236
Node* fn = src;
237237
if (fn->tag == BasicBlock_TAG)
@@ -307,7 +307,7 @@ void postprocess(Parser* p, Module* src, Module* dst) {
307307
};
308308

309309
ctx.rewriter.rewrite_op_fn = (RewriteOpFn) process_op;
310-
ctx.rewriter.config.process_variables = true;
310+
ctx.rewriter.config.process_params = true;
311311
ctx.rewriter.config.search_map = true;
312312
// ctx.rewriter.config.write_map = false;
313313

src/frontends/spirv/s2s.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ size_t parse_spv_instruction_at(SpvParser* parser, size_t instruction_offset) {
890890
assert(s > 0);
891891
if (is_param) {
892892
const Node* param = get_definition_by_id(parser, get_result_defined_at(parser, instruction_offset))->node;
893-
assert(param && param->tag == Variable_TAG);
893+
assert(param && param->tag == Param_TAG);
894894
params = concat_nodes(parser->arena, params, singleton(param));
895895
}
896896
size += s;

src/shady/analysis/free_variables.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ typedef struct {
2323
static void search_op_for_free_variables(Context* visitor, NodeClass class, String op_name, const Node* node) {
2424
assert(node);
2525
switch (node->tag) {
26-
case Variable_TAG: {
26+
case Param_TAG: {
2727
Nodes params = get_abstraction_params(visitor->current_scope->node->node);
2828
for (size_t i = 0; i < params.count; i++) {
2929
if (params.nodes[i] == node)
@@ -32,6 +32,10 @@ static void search_op_for_free_variables(Context* visitor, NodeClass class, Stri
3232
insert_set_get_result(const Node*, visitor->current_scope->free_set, node);
3333
break;
3434
}
35+
case Variablez_TAG: {
36+
insert_set_get_result(const Node*, visitor->current_scope->free_set, node);
37+
break;
38+
}
3539
case Function_TAG:
3640
case Case_TAG:
3741
case BasicBlock_TAG: assert(false);

src/shady/analysis/leak.c

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,6 @@ void visit_enclosing_abstractions(UsesMap* map, const Node* n, void* uptr, Visit
2020
}
2121
}
2222

23-
const Node* get_var_binding_abstraction(const UsesMap* map, const Node* var) {
24-
assert(var->tag == Variable_TAG);
25-
const Use* use = get_first_use(map, var);
26-
assert(use);
27-
const Use* binding_use = NULL;
28-
for (;use; use = use->next_use) {
29-
if (is_abstraction(use->user) && use->operand_class == NcVariable) {
30-
assert(!binding_use);
31-
binding_use = use;
32-
}
33-
}
34-
assert(binding_use && "Failed to find the binding abstraction in the uses map");
35-
return binding_use->user;
36-
}
37-
38-
const Node* get_case_user(const UsesMap* map, const Node* cas) {
39-
const Use* use = get_first_use(map, cas);
40-
if (!use)
41-
return NULL;
42-
assert(!use->next_use);
43-
assert(use->operand_class == NcCase);
44-
return use->user;
45-
}
46-
47-
const Node* get_var_instruction(const UsesMap* map, const Node* var) {
48-
const Node* abs = get_var_binding_abstraction(map, var);
49-
if (!abs || abs->tag != Case_TAG)
50-
return NULL; // variable is not bound by a case
51-
const Node* case_user = get_case_user(map, abs);
52-
if (!case_user || case_user->tag != Let_TAG)
53-
return NULL;
54-
return case_user->payload.let.instruction;
55-
}
56-
5723
bool is_control_static(const UsesMap* map, const Node* control) {
5824
assert(control->tag == Control_TAG);
5925
const Node* inside = control->payload.control.inside;

src/shady/analysis/leak.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
typedef void (VisitEnclosingAbsCallback)(void*, const Use*);
1010
void visit_enclosing_abstractions(UsesMap*, const Node*, void* uptr, VisitEnclosingAbsCallback fn);
1111

12-
const Node* get_var_binding_abstraction(const UsesMap*, const Node* var);
13-
const Node* get_case_user(const UsesMap*, const Node* cas);
14-
15-
const Node* get_var_instruction(const UsesMap*, const Node* var);
16-
1712
bool is_control_static(const UsesMap*, const Node* control);
1813

1914
#endif

src/shady/body_builder.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ BodyBuilder* begin_body(IrArena* a) {
2323
return bb;
2424
}
2525

26+
Node* var(IrArena* arena, const char* name, const Node* instruction, size_t i);
27+
2628
static Nodes create_output_variables(IrArena* a, const Node* value, size_t outputs_count, const Node** output_types, String const output_names[]) {
2729
Nodes types;
2830
if (a->config.check_types) {
@@ -51,7 +53,7 @@ static Nodes create_output_variables(IrArena* a, const Node* value, size_t outpu
5153
LARRAY(Node*, vars, types.count);
5254
for (size_t i = 0; i < types.count; i++) {
5355
String var_name = output_names ? output_names[i] : NULL;
54-
vars[i] = (Node*) var(a, types.nodes[i], var_name);
56+
vars[i] = (Node*) var(a, var_name, value, i);
5557
}
5658

5759
// for (size_t i = 0; i < outputs_count; i++) {

0 commit comments

Comments
 (0)