Skip to content

Commit

Permalink
v0.9.2+luau628
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Jun 1, 2024
1 parent a5b5504 commit 82b9c85
Show file tree
Hide file tree
Showing 43 changed files with 1,111 additions and 959 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "luau0-src"
version = "0.9.1+luau625"
version = "0.9.2+luau628"
authors = ["Aleksandr Orlenko <[email protected]>"]
edition = "2021"
repository = "https://github.com/khvzak/luau-src-rs"
Expand Down
16 changes: 6 additions & 10 deletions luau/Ast/src/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <limits.h>

LUAU_FASTFLAGVARIABLE(LuauLexerLookaheadRemembersBraceType, false)
LUAU_FASTFLAGVARIABLE(LuauCheckedFunctionSyntax, false)

namespace Luau
{
Expand Down Expand Up @@ -995,17 +994,14 @@ Lexeme Lexer::readNext()
}
case '@':
{
if (FFlag::LuauCheckedFunctionSyntax)
{
// We're trying to lex the token @checked
LUAU_ASSERT(peekch() == '@');
// We're trying to lex the token @checked
LUAU_ASSERT(peekch() == '@');

std::pair<AstName, Lexeme::Type> maybeChecked = readName();
if (maybeChecked.second != Lexeme::ReservedChecked)
return Lexeme(Location(start, position()), Lexeme::Error);
std::pair<AstName, Lexeme::Type> maybeChecked = readName();
if (maybeChecked.second != Lexeme::ReservedChecked)
return Lexeme(Location(start, position()), Lexeme::Error);

return Lexeme(Location(start, position()), maybeChecked.second, maybeChecked.first.value);
}
return Lexeme(Location(start, position()), maybeChecked.second, maybeChecked.first.value);
}
default:
if (isDigit(peekch()))
Expand Down
6 changes: 2 additions & 4 deletions luau/Ast/src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ LUAU_FASTINTVARIABLE(LuauParseErrorLimit, 100)
// Warning: If you are introducing new syntax, ensure that it is behind a separate
// flag so that we don't break production games by reverting syntax changes.
// See docs/SyntaxChanges.md for an explanation.
LUAU_FASTFLAG(LuauCheckedFunctionSyntax)
LUAU_FASTFLAGVARIABLE(DebugLuauDeferredConstraintResolution, false)

namespace Luau
Expand Down Expand Up @@ -838,7 +837,7 @@ AstStat* Parser::parseDeclaration(const Location& start)
{
nextLexeme();
bool checkedFunction = false;
if (FFlag::LuauCheckedFunctionSyntax && lexer.current().type == Lexeme::ReservedChecked)
if (lexer.current().type == Lexeme::ReservedChecked)
{
checkedFunction = true;
nextLexeme();
Expand Down Expand Up @@ -1731,9 +1730,8 @@ AstTypeOrPack Parser::parseSimpleType(bool allowPack, bool inDeclarationContext)
{
return {parseTableType(/* inDeclarationContext */ inDeclarationContext), {}};
}
else if (FFlag::LuauCheckedFunctionSyntax && inDeclarationContext && lexer.current().type == Lexeme::ReservedChecked)
else if (inDeclarationContext && lexer.current().type == Lexeme::ReservedChecked)
{
LUAU_ASSERT(FFlag::LuauCheckedFunctionSyntax);
nextLexeme();
return parseFunctionType(allowPack, /* isCheckedFunction */ true);
}
Expand Down
28 changes: 26 additions & 2 deletions luau/CodeGen/include/Luau/CodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

struct lua_State;

#if defined(__x86_64__) || defined(_M_X64)
#define CODEGEN_TARGET_X64
#elif defined(__aarch64__) || defined(_M_ARM64)
#define CODEGEN_TARGET_A64
#endif

namespace Luau
{
namespace CodeGen
Expand Down Expand Up @@ -86,7 +92,7 @@ struct HostIrHooks
// Guards should take a VM exit to 'pcpos'
HostVectorAccessHandler vectorAccess = nullptr;

// Handle namecalled performed on a vector value
// Handle namecall performed on a vector value
// 'sourceReg' (self argument) is guaranteed to be a vector
// All other arguments can be of any type
// Guards should take a VM exit to 'pcpos'
Expand All @@ -97,6 +103,9 @@ struct CompilationOptions
{
unsigned int flags = 0;
HostIrHooks hooks;

// null-terminated array of userdata types names that might have custom lowering
const char* const* userdataTypes = nullptr;
};

struct CompilationStats
Expand Down Expand Up @@ -138,8 +147,17 @@ using UniqueSharedCodeGenContext = std::unique_ptr<SharedCodeGenContext, SharedC
// SharedCodeGenContext must be destroyed before this function is called.
void destroySharedCodeGenContext(const SharedCodeGenContext* codeGenContext) noexcept;

void create(lua_State* L, AllocationCallback* allocationCallback, void* allocationCallbackContext);
// Initializes native code-gen on the provided Luau VM, using a VM-specific
// code-gen context and either the default allocator parameters or custom
// allocator parameters.
void create(lua_State* L);
void create(lua_State* L, AllocationCallback* allocationCallback, void* allocationCallbackContext);
void create(lua_State* L, size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext);

// Initializes native code-gen on the provided Luau VM, using the provided
// SharedCodeGenContext. Note that after this function is called, the
// SharedCodeGenContext must not be destroyed until after the Luau VM L is
// destroyed via lua_close.
void create(lua_State* L, SharedCodeGenContext* codeGenContext);

// Check if native execution is enabled
Expand All @@ -148,6 +166,12 @@ void create(lua_State* L, SharedCodeGenContext* codeGenContext);
// Enable or disable native execution according to `enabled` argument
void setNativeExecutionEnabled(lua_State* L, bool enabled);

// Given a name, this function must return the index of the type which matches the type array used all CompilationOptions and AssemblyOptions
// If the type is unknown, 0xff has to be returned
using UserdataRemapperCallback = uint8_t(void* context, const char* name, size_t nameLength);

void setUserdataRemapper(lua_State* L, void* context, UserdataRemapperCallback cb);

using ModuleId = std::array<uint8_t, 16>;

// Builds target function and all inner functions
Expand Down
6 changes: 4 additions & 2 deletions luau/CodeGen/include/Luau/IrDump.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ void toString(IrToStringContext& ctx, IrOp op);

void toString(std::string& result, IrConst constant);

const char* getBytecodeTypeName(uint8_t type);
const char* getBytecodeTypeName_DEPRECATED(uint8_t type);
const char* getBytecodeTypeName(uint8_t type, const char* const* userdataTypes);

void toString(std::string& result, const BytecodeTypes& bcTypes);
void toString_DEPRECATED(std::string& result, const BytecodeTypes& bcTypes);
void toString(std::string& result, const BytecodeTypes& bcTypes, const char* const* userdataTypes);

void toStringDetailed(
IrToStringContext& ctx, const IrBlock& block, uint32_t blockIdx, const IrInst& inst, uint32_t instIdx, IncludeUseInfo includeUseInfo);
Expand Down
4 changes: 4 additions & 0 deletions luau/CodeGen/include/Luau/IrUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ IrValueKind getCmdValueKind(IrCmd cmd);

bool isGCO(uint8_t tag);

// Optional bit has to be cleared at call site, otherwise, this will return 'false' for 'userdata?'
bool isUserdataBytecodeType(uint8_t ty);
bool isCustomUserdataBytecodeType(uint8_t ty);

// Manually add or remove use of an operand
void addUse(IrFunction& function, IrOp op);
void removeUse(IrFunction& function, IrOp op);
Expand Down
7 changes: 3 additions & 4 deletions luau/CodeGen/include/Luau/UnwindBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace CodeGen
{

// This value is used in 'finishFunction' to mark the function that spans to the end of the whole code block
static uint32_t kFullBlockFuncton = ~0u;
static uint32_t kFullBlockFunction = ~0u;

class UnwindBuilder
{
Expand Down Expand Up @@ -52,11 +52,10 @@ class UnwindBuilder
virtual void prologueX64(uint32_t prologueSize, uint32_t stackSize, bool setupFrame, std::initializer_list<X64::RegisterX64> gpr,
const std::vector<X64::RegisterX64>& simd) = 0;

virtual size_t getSize() const = 0;
virtual size_t getFunctionCount() const = 0;
virtual size_t getUnwindInfoSize(size_t blockSize) const = 0;

// This will place the unwinding data at the target address and might update values of some fields
virtual void finalize(char* target, size_t offset, void* funcAddress, size_t funcSize) const = 0;
virtual size_t finalize(char* target, size_t offset, void* funcAddress, size_t blockSize) const = 0;
};

} // namespace CodeGen
Expand Down
5 changes: 2 additions & 3 deletions luau/CodeGen/include/Luau/UnwindBuilderDwarf2.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ class UnwindBuilderDwarf2 : public UnwindBuilder
void prologueX64(uint32_t prologueSize, uint32_t stackSize, bool setupFrame, std::initializer_list<X64::RegisterX64> gpr,
const std::vector<X64::RegisterX64>& simd) override;

size_t getSize() const override;
size_t getFunctionCount() const override;
size_t getUnwindInfoSize(size_t blockSize = 0) const override;

void finalize(char* target, size_t offset, void* funcAddress, size_t funcSize) const override;
size_t finalize(char* target, size_t offset, void* funcAddress, size_t blockSize) const override;

private:
size_t beginOffset = 0;
Expand Down
5 changes: 2 additions & 3 deletions luau/CodeGen/include/Luau/UnwindBuilderWin.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ class UnwindBuilderWin : public UnwindBuilder
void prologueX64(uint32_t prologueSize, uint32_t stackSize, bool setupFrame, std::initializer_list<X64::RegisterX64> gpr,
const std::vector<X64::RegisterX64>& simd) override;

size_t getSize() const override;
size_t getFunctionCount() const override;
size_t getUnwindInfoSize(size_t blockSize = 0) const override;

void finalize(char* target, size_t offset, void* funcAddress, size_t funcSize) const override;
size_t finalize(char* target, size_t offset, void* funcAddress, size_t blockSize) const override;

private:
size_t beginOffset = 0;
Expand Down
100 changes: 31 additions & 69 deletions luau/CodeGen/src/BytecodeAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@

#include <algorithm>

#include <algorithm>

LUAU_FASTFLAG(LuauCodegenDirectUserdataFlow)
LUAU_FASTFLAG(LuauLoadTypeInfo) // Because new VM typeinfo load changes the format used by Codegen, same flag is used
LUAU_FASTFLAGVARIABLE(LuauCodegenTypeInfo, false) // New analysis is flagged separately
LUAU_FASTFLAG(LuauTypeInfoLookupImprovement)
LUAU_FASTFLAGVARIABLE(LuauCodegenVectorMispredictFix, false)
LUAU_FASTFLAGVARIABLE(LuauCodegenAnalyzeHostVectorOps, false)
LUAU_FASTFLAGVARIABLE(LuauCodegenLoadTypeUpvalCheck, false)

Expand Down Expand Up @@ -70,21 +66,13 @@ void loadBytecodeTypeInfo(IrFunction& function)

Proto* proto = function.proto;

if (FFlag::LuauTypeInfoLookupImprovement)
{
if (!proto)
return;
}
else
{
if (!proto || !proto->typeinfo)
return;
}
if (!proto)
return;

BytecodeTypeInfo& typeInfo = function.bcTypeInfo;

// If there is no typeinfo, we generate default values for arguments and upvalues
if (FFlag::LuauTypeInfoLookupImprovement && !proto->typeinfo)
if (!proto->typeinfo)
{
typeInfo.argumentTypes.resize(proto->numparams, LBC_TYPE_ANY);
typeInfo.upvalueTypes.resize(proto->nups, LBC_TYPE_ANY);
Expand Down Expand Up @@ -152,8 +140,6 @@ void loadBytecodeTypeInfo(IrFunction& function)

static void prepareRegTypeInfoLookups(BytecodeTypeInfo& typeInfo)
{
CODEGEN_ASSERT(FFlag::LuauTypeInfoLookupImprovement);

// Sort by register first, then by end PC
std::sort(typeInfo.regTypes.begin(), typeInfo.regTypes.end(), [](const BytecodeRegTypeInfo& a, const BytecodeRegTypeInfo& b) {
if (a.reg != b.reg)
Expand Down Expand Up @@ -188,39 +174,26 @@ static BytecodeRegTypeInfo* findRegType(BytecodeTypeInfo& info, uint8_t reg, int
{
CODEGEN_ASSERT(FFlag::LuauCodegenTypeInfo);

if (FFlag::LuauTypeInfoLookupImprovement)
{
auto b = info.regTypes.begin() + info.regTypeOffsets[reg];
auto e = info.regTypes.begin() + info.regTypeOffsets[reg + 1];

// Doen't have info
if (b == e)
return nullptr;
auto b = info.regTypes.begin() + info.regTypeOffsets[reg];
auto e = info.regTypes.begin() + info.regTypeOffsets[reg + 1];

// No info after the last live range
if (pc >= (e - 1)->endpc)
return nullptr;

for (auto it = b; it != e; ++it)
{
CODEGEN_ASSERT(it->reg == reg);

if (pc >= it->startpc && pc < it->endpc)
return &*it;
}
// Doen't have info
if (b == e)
return nullptr;

// No info after the last live range
if (pc >= (e - 1)->endpc)
return nullptr;
}
else

for (auto it = b; it != e; ++it)
{
for (BytecodeRegTypeInfo& el : info.regTypes)
{
if (reg == el.reg && pc >= el.startpc && pc < el.endpc)
return &el;
}
CODEGEN_ASSERT(it->reg == reg);

return nullptr;
if (pc >= it->startpc && pc < it->endpc)
return &*it;
}

return nullptr;
}

static void refineRegType(BytecodeTypeInfo& info, uint8_t reg, int pc, uint8_t ty)
Expand All @@ -235,7 +208,7 @@ static void refineRegType(BytecodeTypeInfo& info, uint8_t reg, int pc, uint8_t t
if (regType->type == LBC_TYPE_ANY)
regType->type = ty;
}
else if (FFlag::LuauTypeInfoLookupImprovement && reg < info.argumentTypes.size())
else if (reg < info.argumentTypes.size())
{
if (info.argumentTypes[reg] == LBC_TYPE_ANY)
info.argumentTypes[reg] = ty;
Expand Down Expand Up @@ -629,8 +602,7 @@ void analyzeBytecodeTypes(IrFunction& function, const HostIrHooks& hostHooks)

BytecodeTypeInfo& bcTypeInfo = function.bcTypeInfo;

if (FFlag::LuauTypeInfoLookupImprovement)
prepareRegTypeInfoLookups(bcTypeInfo);
prepareRegTypeInfoLookups(bcTypeInfo);

// Setup our current knowledge of type tags based on arguments
uint8_t regTags[256];
Expand Down Expand Up @@ -788,32 +760,22 @@ void analyzeBytecodeTypes(IrFunction& function, const HostIrHooks& hostHooks)

regTags[ra] = LBC_TYPE_ANY;

if (FFlag::LuauCodegenVectorMispredictFix)
if (bcType.a == LBC_TYPE_VECTOR)
{
if (bcType.a == LBC_TYPE_VECTOR)
{
TString* str = gco2ts(function.proto->k[kc].value.gc);
const char* field = getstr(str);

if (str->len == 1)
{
// Same handling as LOP_GETTABLEKS block in lvmexecute.cpp - case-insensitive comparison with "X" / "Y" / "Z"
char ch = field[0] | ' ';
TString* str = gco2ts(function.proto->k[kc].value.gc);
const char* field = getstr(str);

if (ch == 'x' || ch == 'y' || ch == 'z')
regTags[ra] = LBC_TYPE_NUMBER;
}
if (str->len == 1)
{
// Same handling as LOP_GETTABLEKS block in lvmexecute.cpp - case-insensitive comparison with "X" / "Y" / "Z"
char ch = field[0] | ' ';

if (FFlag::LuauCodegenAnalyzeHostVectorOps && regTags[ra] == LBC_TYPE_ANY && hostHooks.vectorAccessBytecodeType)
regTags[ra] = hostHooks.vectorAccessBytecodeType(field, str->len);
if (ch == 'x' || ch == 'y' || ch == 'z')
regTags[ra] = LBC_TYPE_NUMBER;
}
}
else
{
// Assuming that vector component is being indexed
// TODO: check what key is used
if (bcType.a == LBC_TYPE_VECTOR)
regTags[ra] = LBC_TYPE_NUMBER;

if (FFlag::LuauCodegenAnalyzeHostVectorOps && regTags[ra] == LBC_TYPE_ANY && hostHooks.vectorAccessBytecodeType)
regTags[ra] = hostHooks.vectorAccessBytecodeType(field, str->len);
}

bcType.result = regTags[ra];
Expand Down
Loading

0 comments on commit 82b9c85

Please sign in to comment.