diff --git a/include/daScript/simulate/aot_builtin.h b/include/daScript/simulate/aot_builtin.h index bbfa3a44eb..b5532d5a80 100644 --- a/include/daScript/simulate/aot_builtin.h +++ b/include/daScript/simulate/aot_builtin.h @@ -179,4 +179,7 @@ namespace das { void builtin_main_loop ( const TBlock & block, Context * context, LineInfoArg * at ); vec4f _builtin_hash ( Context & context, SimNode_CallBase * call, vec4f * args ); + + const char * das_get_platform_name(); + const char * das_get_architecture_name(); } diff --git a/modules/dasLLVM/daslib/llvm_boost.das b/modules/dasLLVM/daslib/llvm_boost.das index 795bf39c69..30a3d2205a 100644 --- a/modules/dasLLVM/daslib/llvm_boost.das +++ b/modules/dasLLVM/daslib/llvm_boost.das @@ -176,12 +176,9 @@ def LLVMIsVector3(typ : LLVMOpaqueType?) : bool { def LLVMBuildLoadVector3(builder : LLVMOpaqueBuilder?; var types : PrimitiveTypes?; var typ : LLVMOpaqueType?; ptr : LLVMOpaqueValue?; name : string) { verify(LLVMGetVectorSize(typ) == 3u) let elemT = LLVMGetElementType(typ) - var ld = LLVMBuildLoad2(builder, LLVMVectorType(elemT, 4u), ptr, name) - LLVMSetVolatile(ld, 1) - LLVMSetAlignment(ld, 4u) - var x = LLVMBuildExtractElement(builder, ld, types.ConstI32(0ul), "") - var y = LLVMBuildExtractElement(builder, ld, types.ConstI32(1ul), "") - var z = LLVMBuildExtractElement(builder, ld, types.ConstI32(2ul), "") + var x = LLVMBuildLoad2(builder, elemT, ptr, "") + var y = LLVMBuildLoad2(builder, elemT, LLVMBuildGEP2(builder, elemT, ptr, types.ConstI32(1ul), "", true), "") + var z = LLVMBuildLoad2(builder, elemT, LLVMBuildGEP2(builder, elemT, ptr, types.ConstI32(2ul), "", true), "") var xyz = LLVMGetUndef(LLVMVectorType(elemT, 3u)) xyz = LLVMBuildInsertElement(builder, xyz, x, types.ConstI32(0ul), "") xyz = LLVMBuildInsertElement(builder, xyz, y, types.ConstI32(1ul), "") diff --git a/modules/dasLLVM/daslib/llvm_jit.das b/modules/dasLLVM/daslib/llvm_jit.das index 42d679fb16..238135b6f5 100644 --- a/modules/dasLLVM/daslib/llvm_jit.das +++ b/modules/dasLLVM/daslib/llvm_jit.das @@ -240,7 +240,7 @@ class LlvmJitVisitor : AstVisitor { option_no_alias : bool = false option_no_capture : bool = false own_di : bool = false - ldu_hint : array + ldu_hint : bool attributes : Attributes? def LlvmJitVisitor(ctx : Context?; var types_ : PrimitiveTypes?; uids : UidNodes?; need_di : bool; dib : LLVMOpaqueDIBuilder?; attrs : Attributes?) { @@ -698,7 +698,7 @@ class LlvmJitVisitor : AstVisitor { assert(g_builder != null, "missing builder") thisFunc = get_ptr(fun) uid.reset(get_ptr(fun)) - ldu_hint |> push(LLVM_JIT_ALLOW_UNALIGNED_VECTOR_READ_OUT_OF_BOUNDS) + ldu_hint = LLVM_JIT_ALLOW_UNALIGNED_VECTOR_READ_OUT_OF_BOUNDS let fnmna = uid.get_dll_fn_name(fun) ffunc = LLVMGetNamedFunction(g_mod, fnmna.impl()) wfunc = LLVMGetNamedFunction(g_mod, fnmna.publ()) @@ -731,7 +731,7 @@ class LlvmJitVisitor : AstVisitor { LLVMPositionBuilderAtEnd(g_builder, function_entry) LLVMBuildBr(g_builder, function_body) debug_after_function() - ldu_hint |> pop() + LLVM_JIT_ALLOW_UNALIGNED_VECTOR_READ_OUT_OF_BOUNDS = ldu_hint ffunc = null thisFunc = null uid.reset(null) @@ -6190,6 +6190,9 @@ class JIT_LLVM : AstSimulateMacro { assume path_to_shared_lib = string(prog.policies.jit_path_to_shared_lib) assume path_to_linker = string(prog.policies.jit_path_to_linker) + // Set global options + LLVM_JIT_ALLOW_UNALIGNED_VECTOR_READ_OUT_OF_BOUNDS = prog._options |> find_arg("jit_vec3_ldu") ?as tBool ?? LLVM_JIT_ALLOW_UNALIGNED_VECTOR_READ_OUT_OF_BOUNDS_DEFAULT + prog |> for_each_module <| $(mod) { mod |> for_each_function("") <| $(fun) { var rqj = fun.moreFlags.requestJit @@ -6325,3 +6328,9 @@ class JIT_LLVM : AstSimulateMacro { } +[_macro, macro_function] +def init_llvm_jit_module_options() { + if (is_compiling_macros_in_module("llvm_jit")) { + this_module() |> add_module_option("jit_vec3_ldu", Type.tBool) + } +} diff --git a/modules/dasLLVM/daslib/llvm_jit_common.das b/modules/dasLLVM/daslib/llvm_jit_common.das index 67c96bf514..c3de5b24ec 100644 --- a/modules/dasLLVM/daslib/llvm_jit_common.das +++ b/modules/dasLLVM/daslib/llvm_jit_common.das @@ -30,7 +30,8 @@ let public LLVM_DEBUG_LINE_TRACES = false let public LLVM_JIT_LOG = false || LLVM_DEBUG_EVERYTHING -var public LLVM_JIT_ALLOW_UNALIGNED_VECTOR_READ_OUT_OF_BOUNDS = false +let public LLVM_JIT_ALLOW_UNALIGNED_VECTOR_READ_OUT_OF_BOUNDS_DEFAULT = false // NOTE - this should be true, to match interpreter and AOT behavior +var public LLVM_JIT_ALLOW_UNALIGNED_VECTOR_READ_OUT_OF_BOUNDS = LLVM_JIT_ALLOW_UNALIGNED_VECTOR_READ_OUT_OF_BOUNDS_DEFAULT let public LLVM_ENABLE_OPT_PASS = LLVM_DEBUG_EVERYTHING ? false : true let public LLVM_LOG_RESULT = LLVM_DEBUG_EVERYTHING || LLVM_DEBUG_RESULT @@ -869,9 +870,8 @@ def public build_broadcast_vector(builder : LLVMOpaqueBuilder?; opType : TypeDec } } - def public LLVMBuildLoadData2Aligned(builder : LLVMOpaqueBuilder?; var typ : LLVMOpaqueType?; ptr : LLVMOpaqueValue?; alignment : int; name : string) { +def public LLVMBuildLoadData2Aligned(builder : LLVMOpaqueBuilder?; var typ : LLVMOpaqueType?; ptr : LLVMOpaqueValue?; alignment : int; name : string) { if (!LLVM_JIT_ALLOW_UNALIGNED_VECTOR_READ_OUT_OF_BOUNDS && alignment != 16 && LLVMIsVector3(typ)) { - // slow path return LLVMBuildLoadVector3(builder, g_prim_t, typ, ptr, name) } else { return LLVMBuildLoad2Aligned(builder, typ, ptr, alignment |> uint, name) diff --git a/modules/dasLLVM/profile/nbodies.das b/modules/dasLLVM/profile/nbodies.das index f5d820b3a7..013a62274e 100644 --- a/modules/dasLLVM/profile/nbodies.das +++ b/modules/dasLLVM/profile/nbodies.das @@ -46,7 +46,6 @@ let { nbodies = 5 } -[jit] def offset_momentum(var bodies : body[5]) { var px : float3 for (b in bodies) { @@ -55,7 +54,7 @@ def offset_momentum(var bodies : body[5]) { bodies[0].v = px / SOLAR_MASS } -[jit, hint(alwaysinline, hot, noalias=bodies, unsafe_range_check)] +[hint(vec3_ldu)] def advance(var bodies : body[5]) { unroll <| $() { for (i in range(nbodies)) { @@ -74,7 +73,6 @@ def advance(var bodies : body[5]) { } } -[jit] def energy(var bodies : body[5]) { var e = 0.0 var i = 0 @@ -89,7 +87,6 @@ def energy(var bodies : body[5]) { return e } -[jit] def scale_bodies(scale; var bodies : body[5]) { for (b in bodies) { b.mass *= scale * scale @@ -97,7 +94,6 @@ def scale_bodies(scale; var bodies : body[5]) { } } -[jit] def nbodies(n : int) { scale_bodies(0.01, g_bodies) for (i in range(n)) { diff --git a/src/builtin/module_builtin_runtime.cpp b/src/builtin/module_builtin_runtime.cpp index 8cd1ae6000..0511e928b7 100644 --- a/src/builtin/module_builtin_runtime.cpp +++ b/src/builtin/module_builtin_runtime.cpp @@ -1603,6 +1603,38 @@ namespace das idpi->noPointerCast = true; } + // windows, darwin, linux, etc + const char * das_get_platform_name() { + #if defined(_WIN32) || defined(_WIN64) + return "windows"; + #elif defined(__APPLE__) || defined(__MACH__) + return "darwin"; + #elif defined(__linux__) + return "linux"; + #elif defined(__EMSCRIPTEN__) + return "emscripten"; + #else + return "unknown"; + #endif + } + + // x86, arm, etc + const char * das_get_architecture_name() { + #if defined(__x86_64__) || defined(_M_X64) + return "x86_64"; + #elif defined(__i386) || defined(_M_IX86) + return "x86"; + #elif defined(__aarch64__) + return "arm64"; + #elif defined(__arm__) || defined(_M_ARM) + return "arm"; + #elif defined(__EMSCRIPTEN__) + return "wasm32"; + #else + return "unknown"; + #endif + } + void Module_BuiltIn::addRuntime(ModuleLibrary & lib) { // printer flags addAlias(makePrintFlags()); @@ -2133,5 +2165,10 @@ namespace das addExtern(*this, lib, "__bit_set", SideEffects::modifyArgument, "__bit_set64") ->args({"value","mask","on"}); + // platform and architecture + addExtern(*this, lib, "get_platform_name", + SideEffects::none, "das_get_platform_name"); + addExtern(*this, lib, "get_architecture_name", + SideEffects::none, "das_get_architecture_name"); } }