2022-10-14 20:48:41 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#include "Luau/CodeGen.h"
|
|
|
|
|
|
|
|
#include "Luau/AssemblyBuilderX64.h"
|
|
|
|
#include "Luau/Common.h"
|
|
|
|
#include "Luau/CodeAllocator.h"
|
|
|
|
#include "Luau/CodeBlockUnwind.h"
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-03 19:26:13 +00:00
|
|
|
#include "Luau/IrAnalysis.h"
|
|
|
|
#include "Luau/IrBuilder.h"
|
2023-02-24 21:49:38 +00:00
|
|
|
#include "Luau/OptimizeConstProp.h"
|
2023-02-10 19:40:38 +00:00
|
|
|
#include "Luau/OptimizeFinalX64.h"
|
2022-10-14 20:48:41 +01:00
|
|
|
#include "Luau/UnwindBuilder.h"
|
|
|
|
#include "Luau/UnwindBuilderDwarf2.h"
|
|
|
|
#include "Luau/UnwindBuilderWin.h"
|
|
|
|
|
|
|
|
#include "CustomExecUtils.h"
|
|
|
|
#include "CodeGenX64.h"
|
|
|
|
#include "EmitCommonX64.h"
|
|
|
|
#include "EmitInstructionX64.h"
|
2023-01-27 22:28:31 +00:00
|
|
|
#include "IrLoweringX64.h"
|
2022-10-14 20:48:41 +01:00
|
|
|
#include "NativeState.h"
|
|
|
|
|
|
|
|
#include "lapi.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#if defined(__x86_64__) || defined(_M_X64)
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <intrin.h> // __cpuid
|
|
|
|
#else
|
|
|
|
#include <cpuid.h> // __cpuid
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
LUAU_FASTFLAGVARIABLE(DebugCodegenNoOpt, false)
|
2023-01-27 22:28:31 +00:00
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
namespace CodeGen
|
|
|
|
{
|
|
|
|
|
2022-10-21 18:54:01 +01:00
|
|
|
constexpr uint32_t kFunctionAlignment = 32;
|
|
|
|
|
2022-10-28 11:37:29 +01:00
|
|
|
static void assembleHelpers(AssemblyBuilderX64& build, ModuleHelpers& helpers)
|
|
|
|
{
|
|
|
|
if (build.logText)
|
|
|
|
build.logAppend("; exitContinueVm\n");
|
|
|
|
helpers.exitContinueVm = build.setLabel();
|
|
|
|
emitExit(build, /* continueInVm */ true);
|
|
|
|
|
|
|
|
if (build.logText)
|
|
|
|
build.logAppend("; exitNoContinueVm\n");
|
|
|
|
helpers.exitNoContinueVm = build.setLabel();
|
|
|
|
emitExit(build, /* continueInVm */ false);
|
2022-11-04 17:33:22 +00:00
|
|
|
|
|
|
|
if (build.logText)
|
|
|
|
build.logAppend("; continueCallInVm\n");
|
|
|
|
helpers.continueCallInVm = build.setLabel();
|
|
|
|
emitContinueCallInVm(build);
|
2022-10-28 11:37:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static NativeProto* assembleFunction(AssemblyBuilderX64& build, NativeState& data, ModuleHelpers& helpers, Proto* proto, AssemblyOptions options)
|
2022-10-14 20:48:41 +01:00
|
|
|
{
|
|
|
|
NativeProto* result = new NativeProto();
|
|
|
|
|
|
|
|
result->proto = proto;
|
|
|
|
|
2023-01-27 22:28:31 +00:00
|
|
|
if (options.includeAssembly || options.includeIr)
|
2022-10-14 20:48:41 +01:00
|
|
|
{
|
|
|
|
if (proto->debugname)
|
|
|
|
build.logAppend("; function %s()", getstr(proto->debugname));
|
|
|
|
else
|
|
|
|
build.logAppend("; function()");
|
|
|
|
|
|
|
|
if (proto->linedefined >= 0)
|
|
|
|
build.logAppend(" line %d\n", proto->linedefined);
|
|
|
|
else
|
|
|
|
build.logAppend("\n");
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:54:01 +01:00
|
|
|
build.align(kFunctionAlignment, AlignmentDataX64::Ud2);
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
Label start = build.setLabel();
|
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
IrBuilder builder;
|
|
|
|
builder.buildFunctionIr(proto);
|
2022-10-21 18:54:01 +01:00
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
if (!FFlag::DebugCodegenNoOpt)
|
2022-10-28 11:37:29 +01:00
|
|
|
{
|
2023-02-24 21:49:38 +00:00
|
|
|
constPropInBlockChains(builder);
|
2022-10-28 11:37:29 +01:00
|
|
|
}
|
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
optimizeMemoryOperandsX64(builder.function);
|
2022-10-21 18:54:01 +01:00
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
IrLoweringX64 lowering(build, helpers, data, proto, builder.function);
|
2022-10-21 18:54:01 +01:00
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
lowering.lower(options);
|
2022-10-21 18:54:01 +01:00
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
result->instTargets = new uintptr_t[proto->sizecode];
|
2022-10-21 18:54:01 +01:00
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
for (int i = 0; i < proto->sizecode; i++)
|
2022-10-21 18:54:01 +01:00
|
|
|
{
|
2023-02-24 21:49:38 +00:00
|
|
|
auto [irLocation, asmLocation] = builder.function.bcMapping[i];
|
2022-10-21 18:54:01 +01:00
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
result->instTargets[i] = irLocation == ~0u ? 0 : asmLocation - start.location;
|
2022-10-21 18:54:01 +01:00
|
|
|
}
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
result->location = start.location;
|
|
|
|
|
|
|
|
if (build.logText)
|
|
|
|
build.logAppend("\n");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroyNativeProto(NativeProto* nativeProto)
|
|
|
|
{
|
|
|
|
delete[] nativeProto->instTargets;
|
|
|
|
delete nativeProto;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void onCloseState(lua_State* L)
|
|
|
|
{
|
|
|
|
destroyNativeState(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void onDestroyFunction(lua_State* L, Proto* proto)
|
|
|
|
{
|
|
|
|
NativeProto* nativeProto = getProtoExecData(proto);
|
|
|
|
LUAU_ASSERT(nativeProto->proto == proto);
|
|
|
|
|
|
|
|
setProtoExecData(proto, nullptr);
|
|
|
|
destroyNativeProto(nativeProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int onEnter(lua_State* L, Proto* proto)
|
|
|
|
{
|
|
|
|
if (L->singlestep)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
NativeState* data = getNativeState(L);
|
|
|
|
|
|
|
|
if (!L->ci->savedpc)
|
|
|
|
L->ci->savedpc = proto->code;
|
|
|
|
|
|
|
|
// We will jump into native code through a gateway
|
|
|
|
bool (*gate)(lua_State*, Proto*, uintptr_t, NativeContext*) = (bool (*)(lua_State*, Proto*, uintptr_t, NativeContext*))data->context.gateEntry;
|
|
|
|
|
|
|
|
NativeProto* nativeProto = getProtoExecData(proto);
|
|
|
|
uintptr_t target = nativeProto->instTargets[L->ci->savedpc - proto->code];
|
|
|
|
|
|
|
|
// Returns 1 to finish the function in the VM
|
|
|
|
return gate(L, proto, target, &data->context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void onSetBreakpoint(lua_State* L, Proto* proto, int instruction)
|
|
|
|
{
|
|
|
|
if (!getProtoExecData(proto))
|
|
|
|
return;
|
|
|
|
|
|
|
|
LUAU_ASSERT(!"native breakpoints are not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isSupported()
|
|
|
|
{
|
|
|
|
#if !LUA_CUSTOM_EXECUTION
|
|
|
|
return false;
|
|
|
|
#elif defined(__x86_64__) || defined(_M_X64)
|
|
|
|
if (LUA_EXTRA_SIZE != 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (sizeof(TValue) != 16)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (sizeof(LuaNode) != 32)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int cpuinfo[4] = {};
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
__cpuid(cpuinfo, 1);
|
|
|
|
#else
|
|
|
|
__cpuid(1, cpuinfo[0], cpuinfo[1], cpuinfo[2], cpuinfo[3]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// We require AVX1 support for VEX encoded XMM operations
|
|
|
|
// We also requre SSE4.1 support for ROUNDSD but the AVX check below covers it
|
|
|
|
// https://en.wikipedia.org/wiki/CPUID#EAX=1:_Processor_Info_and_Feature_Bits
|
|
|
|
if ((cpuinfo[2] & (1 << 28)) == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void create(lua_State* L)
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(isSupported());
|
|
|
|
|
|
|
|
NativeState& data = *createNativeState(L);
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
data.unwindBuilder = std::make_unique<UnwindBuilderWin>();
|
|
|
|
#else
|
|
|
|
data.unwindBuilder = std::make_unique<UnwindBuilderDwarf2>();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
data.codeAllocator.context = data.unwindBuilder.get();
|
|
|
|
data.codeAllocator.createBlockUnwindInfo = createBlockUnwindInfo;
|
|
|
|
data.codeAllocator.destroyBlockUnwindInfo = destroyBlockUnwindInfo;
|
|
|
|
|
|
|
|
initFallbackTable(data);
|
|
|
|
initHelperFunctions(data);
|
|
|
|
|
|
|
|
if (!x64::initEntryFunction(data))
|
|
|
|
{
|
|
|
|
destroyNativeState(L);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_ExecutionCallbacks* ecb = getExecutionCallbacks(L);
|
|
|
|
|
|
|
|
ecb->close = onCloseState;
|
|
|
|
ecb->destroy = onDestroyFunction;
|
|
|
|
ecb->enter = onEnter;
|
|
|
|
ecb->setbreakpoint = onSetBreakpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gatherFunctions(std::vector<Proto*>& results, Proto* proto)
|
|
|
|
{
|
|
|
|
if (results.size() <= size_t(proto->bytecodeid))
|
|
|
|
results.resize(proto->bytecodeid + 1);
|
|
|
|
|
|
|
|
// Skip protos that we've already compiled in this run: this happens because at -O2, inlined functions get their protos reused
|
|
|
|
if (results[proto->bytecodeid])
|
|
|
|
return;
|
|
|
|
|
|
|
|
results[proto->bytecodeid] = proto;
|
|
|
|
|
|
|
|
for (int i = 0; i < proto->sizep; i++)
|
|
|
|
gatherFunctions(results, proto->p[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void compile(lua_State* L, int idx)
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(lua_isLfunction(L, idx));
|
|
|
|
const TValue* func = luaA_toobject(L, idx);
|
|
|
|
|
|
|
|
// If initialization has failed, do not compile any functions
|
|
|
|
if (!getNativeState(L))
|
|
|
|
return;
|
|
|
|
|
|
|
|
AssemblyBuilderX64 build(/* logText= */ false);
|
|
|
|
NativeState* data = getNativeState(L);
|
|
|
|
|
|
|
|
std::vector<Proto*> protos;
|
|
|
|
gatherFunctions(protos, clvalue(func)->l.p);
|
|
|
|
|
2022-10-28 11:37:29 +01:00
|
|
|
ModuleHelpers helpers;
|
|
|
|
assembleHelpers(build, helpers);
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
std::vector<NativeProto*> results;
|
|
|
|
results.reserve(protos.size());
|
|
|
|
|
|
|
|
// Skip protos that have been compiled during previous invocations of CodeGen::compile
|
|
|
|
for (Proto* p : protos)
|
|
|
|
if (p && getProtoExecData(p) == nullptr)
|
2022-10-28 11:37:29 +01:00
|
|
|
results.push_back(assembleFunction(build, *data, helpers, p, {}));
|
2022-10-14 20:48:41 +01:00
|
|
|
|
|
|
|
build.finalize();
|
|
|
|
|
|
|
|
uint8_t* nativeData = nullptr;
|
|
|
|
size_t sizeNativeData = 0;
|
|
|
|
uint8_t* codeStart = nullptr;
|
|
|
|
if (!data->codeAllocator.allocate(
|
|
|
|
build.data.data(), int(build.data.size()), build.code.data(), int(build.code.size()), nativeData, sizeNativeData, codeStart))
|
|
|
|
{
|
|
|
|
for (NativeProto* result : results)
|
|
|
|
destroyNativeProto(result);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relocate instruction offsets
|
|
|
|
for (NativeProto* result : results)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < result->proto->sizecode; i++)
|
|
|
|
result->instTargets[i] += uintptr_t(codeStart + result->location);
|
2022-11-04 17:33:22 +00:00
|
|
|
|
|
|
|
LUAU_ASSERT(result->proto->sizecode);
|
|
|
|
result->entryTarget = result->instTargets[0];
|
2022-10-14 20:48:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Link native proto objects to Proto; the memory is now managed by VM and will be freed via onDestroyFunction
|
|
|
|
for (NativeProto* result : results)
|
|
|
|
setProtoExecData(result->proto, result);
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:54:01 +01:00
|
|
|
std::string getAssembly(lua_State* L, int idx, AssemblyOptions options)
|
2022-10-14 20:48:41 +01:00
|
|
|
{
|
|
|
|
LUAU_ASSERT(lua_isLfunction(L, idx));
|
|
|
|
const TValue* func = luaA_toobject(L, idx);
|
|
|
|
|
2023-01-27 22:28:31 +00:00
|
|
|
AssemblyBuilderX64 build(/* logText= */ options.includeAssembly);
|
2022-10-21 18:54:01 +01:00
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
NativeState data;
|
|
|
|
initFallbackTable(data);
|
|
|
|
|
|
|
|
std::vector<Proto*> protos;
|
|
|
|
gatherFunctions(protos, clvalue(func)->l.p);
|
|
|
|
|
2022-10-28 11:37:29 +01:00
|
|
|
ModuleHelpers helpers;
|
|
|
|
assembleHelpers(build, helpers);
|
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
for (Proto* p : protos)
|
|
|
|
if (p)
|
|
|
|
{
|
2022-10-28 11:37:29 +01:00
|
|
|
NativeProto* nativeProto = assembleFunction(build, data, helpers, p, options);
|
2022-10-14 20:48:41 +01:00
|
|
|
destroyNativeProto(nativeProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
build.finalize();
|
|
|
|
|
2022-10-21 18:54:01 +01:00
|
|
|
if (options.outputBinary)
|
|
|
|
return std::string(build.code.begin(), build.code.end()) + std::string(build.data.begin(), build.data.end());
|
|
|
|
else
|
|
|
|
return build.text;
|
2022-10-14 20:48:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace CodeGen
|
|
|
|
} // namespace Luau
|