authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-06-01 21:54:54+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-09 00:22:17-04:00
log601e831f1d15ba5c8f89fbab265a72bfabb6af96
tree2883351a9ebaa4759b239bff24090133e5f8961c
parentce3f0077cf3c5ae8edc116177f2f3d33258be9f7

Add builtin for llvm.wasm.memory.grow.i32 intrinsic

This will allow the developer to request additional memory pages from the runtime to be allocated for the Wasm app. Typical usage: ```zig var wasm_pages = @wasmMemorySize(); @wasmMemoryGrow(1); @import("std").debug.assert((wasm_pages + 1) == @wasmMemorySize()); ```

4 files changed, 166 insertions(+), 22 deletions(-)

src/all_types.hpp+18-2
......@@ -1826,6 +1826,7 @@ enum BuiltinFnId {
18261826 BuiltinFnIdCall,
18271827 BuiltinFnIdBitSizeof,
18281828 BuiltinFnIdWasmMemorySize,
1829 BuiltinFnIdWasmMemoryGrow,
18291830};
18301831
18311832struct BuiltinFnEntry {
......@@ -2077,6 +2078,7 @@ struct CodeGen {
20772078 LLVMValueRef safety_crash_err_fn;
20782079 LLVMValueRef return_err_fn;
20792080 LLVMValueRef wasm_memory_size;
2081 LLVMValueRef wasm_memory_grow;
20802082 LLVMTypeRef anyframe_fn_type;
20812083
20822084 // reminder: hash tables must be initialized before use
......@@ -2751,6 +2753,7 @@ enum IrInstSrcId {
27512753 IrInstSrcIdSpillBegin,
27522754 IrInstSrcIdSpillEnd,
27532755 IrInstSrcIdWasmMemorySize,
2756 IrInstSrcIdWasmMemoryGrow,
27542757};
27552758
27562759// ir_render_* functions in codegen.cpp consume Gen instructions and produce LLVM IR.
......@@ -2844,6 +2847,7 @@ enum IrInstGenId {
28442847 IrInstGenIdAlloca,
28452848 IrInstGenIdConst,
28462849 IrInstGenIdWasmMemorySize,
2850 IrInstGenIdWasmMemoryGrow,
28472851};
28482852
28492853// Common fields between IrInstSrc and IrInstGen. This allows future passes
......@@ -3732,11 +3736,23 @@ struct IrInstGenMemcpy {
37323736};
37333737
37343738struct IrInstSrcWasmMemorySize {
3735 IrInstSrc base;
3739 IrInstSrc base;
37363740};
37373741
37383742struct IrInstGenWasmMemorySize {
3739 IrInstGen base;
3743 IrInstGen base;
3744};
3745
3746struct IrInstSrcWasmMemoryGrow {
3747 IrInstSrc base;
3748
3749 IrInstSrc *delta;
3750};
3751
3752struct IrInstGenWasmMemoryGrow {
3753 IrInstGen base;
3754
3755 IrInstGen *delta;
37403756};
37413757
37423758struct IrInstSrcSlice {
src/codegen.cpp+38-1
......@@ -1047,8 +1047,9 @@ static void gen_assertion(CodeGen *g, PanicMsgId msg_id, IrInstGen *source_instr
10471047
10481048static LLVMValueRef gen_wasm_memory_size(CodeGen *g) {
10491049 if (g->wasm_memory_size)
1050 return g->wasm_memory_size;
1050 return g->wasm_memory_size;
10511051
1052 // TODO adjust for wasm64 as well
10521053 // declare i32 @llvm.wasm.memory.size.i32(i32) nounwind readonly
10531054 LLVMTypeRef param_type = LLVMInt32Type();
10541055 LLVMTypeRef fn_type = LLVMFunctionType(LLVMInt32Type(), &param_type, 1, false);
......@@ -1058,6 +1059,23 @@ static LLVMValueRef gen_wasm_memory_size(CodeGen *g) {
10581059 return g->wasm_memory_size;
10591060}
10601061
1062static LLVMValueRef gen_wasm_memory_grow(CodeGen *g) {
1063 if (g->wasm_memory_grow)
1064 return g->wasm_memory_grow;
1065
1066 // TODO adjust for wasm64 as well
1067 // declare i32 @llvm.wasm.memory.grow.i32(i32, i32) nounwind
1068 LLVMTypeRef param_types[] = {
1069 LLVMInt32Type(),
1070 LLVMInt32Type(),
1071 };
1072 LLVMTypeRef fn_type = LLVMFunctionType(LLVMInt32Type(), param_types, 2, false);
1073 g->wasm_memory_grow = LLVMAddFunction(g->module, "llvm.wasm.memory.grow.i32", fn_type);
1074 assert(LLVMGetIntrinsicID(g->wasm_memory_grow));
1075
1076 return g->wasm_memory_grow;
1077}
1078
10611079static LLVMValueRef get_stacksave_fn_val(CodeGen *g) {
10621080 if (g->stacksave_fn_val)
10631081 return g->stacksave_fn_val;
......@@ -5607,11 +5625,27 @@ static LLVMValueRef ir_render_wasm_memory_size(CodeGen *g, IrExecutableGen *exec
56075625 //
56085626 // More info:
56095627 // https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#current-linear-memory-size
5628 // TODO adjust for wasm64
56105629 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type);
56115630 LLVMValueRef val = LLVMBuildCall(g->builder, gen_wasm_memory_size(g), &zero, 1, "");
56125631 return val;
56135632}
56145633
5634static LLVMValueRef ir_render_wasm_memory_grow(CodeGen *g, IrExecutableGen *executable, IrInstGenWasmMemoryGrow *instruction) {
5635 // When Wasm lands multi-memory support, we can relax this to permit the user specify
5636 // memory index to inquire about. For now, we pass in the recommended default of index 0.
5637 //
5638 // More info:
5639 // https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#grow-linear-memory-size
5640 // TODO adjust for wasm64
5641 LLVMValueRef params[] = {
5642 LLVMConstNull(g->builtin_types.entry_i32->llvm_type),
5643 ir_llvm_value(g, instruction->delta),
5644 };
5645 LLVMValueRef val = LLVMBuildCall(g->builder, gen_wasm_memory_grow(g), params, 2, "");
5646 return val;
5647}
5648
56155649static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrInstGenSlice *instruction) {
56165650 Error err;
56175651
......@@ -6824,6 +6858,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutableGen *executabl
68246858 return ir_render_vector_extract_elem(g, executable, (IrInstGenVectorExtractElem *) instruction);
68256859 case IrInstGenIdWasmMemorySize:
68266860 return ir_render_wasm_memory_size(g, executable, (IrInstGenWasmMemorySize *) instruction);
6861 case IrInstGenIdWasmMemoryGrow:
6862 return ir_render_wasm_memory_grow(g, executable, (IrInstGenWasmMemoryGrow *) instruction);
68276863 }
68286864 zig_unreachable();
68296865}
......@@ -8687,6 +8723,7 @@ static void define_builtin_fns(CodeGen *g) {
86878723 create_builtin_fn(g, BuiltinFnIdCall, "call", 3);
86888724 create_builtin_fn(g, BuiltinFnIdBitSizeof, "bitSizeOf", 1);
86898725 create_builtin_fn(g, BuiltinFnIdWasmMemorySize, "wasmMemorySize", 0);
8726 create_builtin_fn(g, BuiltinFnIdWasmMemoryGrow, "wasmMemoryGrow", 1);
86908727}
86918728
86928729static const char *bool_to_str(bool b) {
src/ir.cpp+88-19
......@@ -558,6 +558,8 @@ static void destroy_instruction_src(IrInstSrc *inst) {
558558 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCallArgs *>(inst));
559559 case IrInstSrcIdWasmMemorySize:
560560 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcWasmMemorySize *>(inst));
561 case IrInstSrcIdWasmMemoryGrow:
562 return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcWasmMemoryGrow *>(inst));
561563 }
562564 zig_unreachable();
563565}
......@@ -740,6 +742,8 @@ void destroy_instruction_gen(IrInstGen *inst) {
740742 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenNegationWrapping *>(inst));
741743 case IrInstGenIdWasmMemorySize:
742744 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenWasmMemorySize *>(inst));
745 case IrInstGenIdWasmMemoryGrow:
746 return heap::c_allocator.destroy(reinterpret_cast<IrInstGenWasmMemoryGrow *>(inst));
743747 }
744748 zig_unreachable();
745749}
......@@ -1338,10 +1342,6 @@ static constexpr IrInstSrcId ir_inst_id(IrInstSrcBoolNot *) {
13381342 return IrInstSrcIdBoolNot;
13391343}
13401344
1341static constexpr IrInstSrcId ir_inst_id(IrInstSrcWasmMemorySize *) {
1342 return IrInstSrcIdWasmMemorySize;
1343}
1344
13451345static constexpr IrInstSrcId ir_inst_id(IrInstSrcMemset *) {
13461346 return IrInstSrcIdMemset;
13471347}
......@@ -1618,6 +1618,14 @@ static constexpr IrInstSrcId ir_inst_id(IrInstSrcSpillEnd *) {
16181618 return IrInstSrcIdSpillEnd;
16191619}
16201620
1621static constexpr IrInstSrcId ir_inst_id(IrInstSrcWasmMemorySize *) {
1622 return IrInstSrcIdWasmMemorySize;
1623}
1624
1625static constexpr IrInstSrcId ir_inst_id(IrInstSrcWasmMemoryGrow *) {
1626 return IrInstSrcIdWasmMemoryGrow;
1627}
1628
16211629
16221630static constexpr IrInstGenId ir_inst_id(IrInstGenDeclVar *) {
16231631 return IrInstGenIdDeclVar;
......@@ -1967,6 +1975,10 @@ static constexpr IrInstGenId ir_inst_id(IrInstGenWasmMemorySize *) {
19671975 return IrInstGenIdWasmMemorySize;
19681976}
19691977
1978static constexpr IrInstGenId ir_inst_id(IrInstGenWasmMemoryGrow *) {
1979 return IrInstGenIdWasmMemoryGrow;
1980}
1981
19701982template<typename T>
19711983static T *ir_create_instruction(IrBuilderSrc *irb, Scope *scope, AstNode *source_node) {
19721984 T *special_instruction = heap::c_allocator.create<T>();
......@@ -3654,20 +3666,6 @@ static IrInstGen *ir_build_bool_not_gen(IrAnalyze *ira, IrInst *source_instr, Ir
36543666 return &instruction->base;
36553667}
36563668
3657static IrInstSrc *ir_build_wasm_memory_size_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node) {
3658 IrInstSrcWasmMemorySize *instruction = ir_build_instruction<IrInstSrcWasmMemorySize>(irb, scope, source_node);
3659
3660 return &instruction->base;
3661}
3662
3663static IrInstGen *ir_build_wasm_memory_size_gen(IrAnalyze *ira, IrInst *source_instr) {
3664 IrInstGenWasmMemorySize *instruction = ir_build_inst_gen<IrInstGenWasmMemorySize>(&ira->new_irb,
3665 source_instr->scope, source_instr->source_node);
3666 instruction->base.value->type = ira->codegen->builtin_types.entry_i32;
3667
3668 return &instruction->base;
3669}
3670
36713669static IrInstSrc *ir_build_memset_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node,
36723670 IrInstSrc *dest_ptr, IrInstSrc *byte, IrInstSrc *count)
36733671{
......@@ -4987,6 +4985,41 @@ static IrInstGen *ir_build_vector_extract_elem(IrAnalyze *ira, IrInst *source_in
49874985 return &instruction->base;
49884986}
49894987
4988static IrInstSrc *ir_build_wasm_memory_size_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node) {
4989 IrInstSrcWasmMemorySize *instruction = ir_build_instruction<IrInstSrcWasmMemorySize>(irb, scope, source_node);
4990
4991 return &instruction->base;
4992}
4993
4994static IrInstGen *ir_build_wasm_memory_size_gen(IrAnalyze *ira, IrInst *source_instr) {
4995 IrInstGenWasmMemorySize *instruction = ir_build_inst_gen<IrInstGenWasmMemorySize>(&ira->new_irb,
4996 source_instr->scope, source_instr->source_node);
4997 instruction->base.value->type = ira->codegen->builtin_types.entry_i32;
4998
4999 return &instruction->base;
5000}
5001
5002static IrInstSrc *ir_build_wasm_memory_grow_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, IrInstSrc *delta) {
5003 IrInstSrcWasmMemoryGrow *instruction = ir_build_instruction<IrInstSrcWasmMemoryGrow>(irb, scope, source_node);
5004 instruction->delta = delta;
5005
5006 ir_ref_instruction(delta, irb->current_basic_block);
5007
5008 return &instruction->base;
5009}
5010
5011static IrInstGen *ir_build_wasm_memory_grow_gen(IrAnalyze *ira, IrInst *source_instr, IrInstGen *delta) {
5012 IrInstGenWasmMemoryGrow *instruction = ir_build_inst_gen<IrInstGenWasmMemoryGrow>(&ira->new_irb,
5013 source_instr->scope, source_instr->source_node);
5014 instruction->base.value->type = ira->codegen->builtin_types.entry_i32;
5015 instruction->delta = delta;
5016
5017 ir_ref_inst_gen(delta);
5018
5019 return &instruction->base;
5020}
5021
5022
49905023static void ir_count_defers(IrBuilderSrc *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) {
49915024 results[ReturnKindUnconditional] = 0;
49925025 results[ReturnKindError] = 0;
......@@ -6785,6 +6818,16 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
67856818 IrInstSrc *ir_wasm_memory_size = ir_build_wasm_memory_size_src(irb, scope, node);
67866819 return ir_lval_wrap(irb, scope, ir_wasm_memory_size, lval, result_loc);
67876820 }
6821 case BuiltinFnIdWasmMemoryGrow:
6822 {
6823 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
6824 IrInstSrc *arg0_value = ir_gen_node(irb, arg0_node, scope);
6825 if (arg0_value == irb->codegen->invalid_inst_src)
6826 return arg0_value;
6827
6828 IrInstSrc *ir_wasm_memory_grow = ir_build_wasm_memory_grow_src(irb, scope, node, arg0_value);
6829 return ir_lval_wrap(irb, scope, ir_wasm_memory_grow, lval, result_loc);
6830 }
67886831 case BuiltinFnIdField:
67896832 {
67906833 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
......@@ -27683,15 +27726,37 @@ static IrInstGen *ir_analyze_instruction_has_field(IrAnalyze *ira, IrInstSrcHasF
2768327726}
2768427727
2768527728static IrInstGen *ir_analyze_instruction_wasm_memory_size(IrAnalyze *ira, IrInstSrcWasmMemorySize *instruction) {
27729 // TODO generate compile error for target_arch different than 32bit
2768627730 if (!target_is_wasm(ira->codegen->zig_target)) {
2768727731 ir_add_error_node(ira, instruction->base.base.source_node,
27688 buf_sprintf("@wasmMemorySize is a wasm feature only"));
27732 buf_sprintf("@wasmMemorySize is a wasm32 feature only"));
2768927733 return ira->codegen->invalid_inst_gen;
2769027734 }
2769127735
2769227736 return ir_build_wasm_memory_size_gen(ira, &instruction->base.base);
2769327737}
2769427738
27739static IrInstGen *ir_analyze_instruction_wasm_memory_grow(IrAnalyze *ira, IrInstSrcWasmMemoryGrow *instruction) {
27740 // TODO generate compile error for target_arch different than 32bit
27741 if (!target_is_wasm(ira->codegen->zig_target)) {
27742 ir_add_error_node(ira, instruction->base.base.source_node,
27743 buf_sprintf("@wasmMemoryGrow is a wasm32 feature only"));
27744 return ira->codegen->invalid_inst_gen;
27745 }
27746
27747 IrInstGen *delta = instruction->delta->child;
27748 if (type_is_invalid(delta->value->type))
27749 return ira->codegen->invalid_inst_gen;
27750
27751 ZigType *i32_type = ira->codegen->builtin_types.entry_i32;
27752
27753 IrInstGen *casted_delta = ir_implicit_cast(ira, delta, i32_type);
27754 if (type_is_invalid(casted_delta->value->type))
27755 return ira->codegen->invalid_inst_gen;
27756
27757 return ir_build_wasm_memory_grow_gen(ira, &instruction->base.base, casted_delta);
27758}
27759
2769527760static IrInstGen *ir_analyze_instruction_breakpoint(IrAnalyze *ira, IrInstSrcBreakpoint *instruction) {
2769627761 return ir_build_breakpoint_gen(ira, &instruction->base.base);
2769727762}
......@@ -30935,6 +31000,8 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc
3093531000 return ir_analyze_instruction_spill_end(ira, (IrInstSrcSpillEnd *)instruction);
3093631001 case IrInstSrcIdWasmMemorySize:
3093731002 return ir_analyze_instruction_wasm_memory_size(ira, (IrInstSrcWasmMemorySize *)instruction);
31003 case IrInstSrcIdWasmMemoryGrow:
31004 return ir_analyze_instruction_wasm_memory_grow(ira, (IrInstSrcWasmMemoryGrow *)instruction);
3093831005 }
3093931006 zig_unreachable();
3094031007}
......@@ -31111,6 +31178,7 @@ bool ir_inst_gen_has_side_effects(IrInstGen *instruction) {
3111131178 case IrInstGenIdResume:
3111231179 case IrInstGenIdAwait:
3111331180 case IrInstGenIdSpillBegin:
31181 case IrInstGenIdWasmMemoryGrow:
3111431182 return true;
3111531183
3111631184 case IrInstGenIdPhi:
......@@ -31243,6 +31311,7 @@ bool ir_inst_src_has_side_effects(IrInstSrc *instruction) {
3124331311 case IrInstSrcIdResume:
3124431312 case IrInstSrcIdAwait:
3124531313 case IrInstSrcIdSpillBegin:
31314 case IrInstSrcIdWasmMemoryGrow:
3124631315 return true;
3124731316
3124831317 case IrInstSrcIdPhi:
src/ir_print.cpp+22
......@@ -323,6 +323,8 @@ const char* ir_inst_src_type_str(IrInstSrcId id) {
323323 return "SrcSpillEnd";
324324 case IrInstSrcIdWasmMemorySize:
325325 return "SrcWasmMemorySize";
326 case IrInstSrcIdWasmMemoryGrow:
327 return "SrcWasmMemoryGrow";
326328 }
327329 zig_unreachable();
328330}
......@@ -505,6 +507,8 @@ const char* ir_inst_gen_type_str(IrInstGenId id) {
505507 return "GenNegationWrapping";
506508 case IrInstGenIdWasmMemorySize:
507509 return "GenWasmMemorySize";
510 case IrInstGenIdWasmMemoryGrow:
511 return "GenWasmMemoryGrow";
508512 }
509513 zig_unreachable();
510514}
......@@ -1720,6 +1724,18 @@ static void ir_print_wasm_memory_size(IrPrintGen *irp, IrInstGenWasmMemorySize *
17201724 fprintf(irp->f, "@wasmMemorySize()");
17211725}
17221726
1727static void ir_print_wasm_memory_grow(IrPrintSrc *irp, IrInstSrcWasmMemoryGrow *instruction) {
1728 fprintf(irp->f, "@wasmMemoryGrow(");
1729 ir_print_other_inst_src(irp, instruction->delta);
1730 fprintf(irp->f, ")");
1731}
1732
1733static void ir_print_wasm_memory_grow(IrPrintGen *irp, IrInstGenWasmMemoryGrow *instruction) {
1734 fprintf(irp->f, "@wasmMemoryGrow(");
1735 ir_print_other_inst_gen(irp, instruction->delta);
1736 fprintf(irp->f, ")");
1737}
1738
17231739static void ir_print_memset(IrPrintSrc *irp, IrInstSrcMemset *instruction) {
17241740 fprintf(irp->f, "@memset(");
17251741 ir_print_other_inst_src(irp, instruction->dest_ptr);
......@@ -2967,6 +2983,9 @@ static void ir_print_inst_src(IrPrintSrc *irp, IrInstSrc *instruction, bool trai
29672983 case IrInstSrcIdWasmMemorySize:
29682984 ir_print_wasm_memory_size(irp, (IrInstSrcWasmMemorySize *)instruction);
29692985 break;
2986 case IrInstSrcIdWasmMemoryGrow:
2987 ir_print_wasm_memory_grow(irp, (IrInstSrcWasmMemoryGrow *)instruction);
2988 break;
29702989 }
29712990 fprintf(irp->f, "\n");
29722991}
......@@ -3237,6 +3256,9 @@ static void ir_print_inst_gen(IrPrintGen *irp, IrInstGen *instruction, bool trai
32373256 case IrInstGenIdWasmMemorySize:
32383257 ir_print_wasm_memory_size(irp, (IrInstGenWasmMemorySize *)instruction);
32393258 break;
3259 case IrInstGenIdWasmMemoryGrow:
3260 ir_print_wasm_memory_grow(irp, (IrInstGenWasmMemoryGrow *)instruction);
3261 break;
32403262 }
32413263 fprintf(irp->f, "\n");
32423264}