Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
drblallo committed Dec 11, 2024
1 parent bc67ce8 commit c70c878
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 29 deletions.
47 changes: 36 additions & 11 deletions lib/conversions/src/RLCToC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,21 @@ static bool unhandledType(mlir::Type t)
return t.isa<mlir::rlc::OwningPtrType>() or t.isa<mlir::rlc::ArrayType>();
};

llvm::StringRef godotVariantBuiltinTypesName(mlir::Type type)
{
if (type.isa<mlir::rlc::StringLiteralType>())
return "godot::Variant::STRING";
if (type.isa<mlir::rlc::BoolType>())
return "godot::Variant::BOOL";
if (type.isa<mlir::rlc::IntegerType>())
return "godot::Variant::INT";
if (type.isa<mlir::rlc::FloatType>())
return "godot::Variant::FLOAT";

llvm_unreachable("no kwown type");
return "TYPEUNWKOWN";
}

static void emitGodotFunction(
llvm::ArrayRef<mlir::FunctionType> overloadTypes,
llvm::StringRef name,
Expand Down Expand Up @@ -906,14 +921,9 @@ static void emitGodotFunction(
<< typeToString(fType.getInput(I), false) << ">(*((godot::Ref<RLC"
<< typeToString(fType.getInput(I), false) << ">)(*(args[" << I
<< "]))))";
else if (type.isa<mlir::rlc::StringLiteralType>())
OS << " && (*(args[" << I << "])).get_type() == godot::Variant::STRING";
else if (type.isa<mlir::rlc::BoolType>())
OS << " && (*(args[" << I << "])).get_type() == godot::Variant::BOOL";
else if (type.isa<mlir::rlc::IntegerType>())
OS << " && (*(args[" << I << "])).get_type() == godot::Variant::INT";
else if (type.isa<mlir::rlc::FloatType>())
OS << " && (*(args[" << I << "])).get_type() == godot::Variant::FLOAT";
else
OS << " && (*(args[" << I
<< "])).get_type() == " << godotVariantBuiltinTypesName(type);
}

OS << ") {\n";
Expand Down Expand Up @@ -1272,10 +1282,21 @@ class %s: public godot::RefCounted {
if (unhandledType(type))
continue;

auto isUserDefiend = type.isa<mlir::rlc::ClassType>() or
type.isa<mlir::rlc::AlternativeType>();
auto name = mlir::rlc::typeToMangled(type);
OS << "if(auto casted = godot::Object::cast_to<RLC" << name
<< ">(other)){\n";
OS << "*content = *((*casted).content);\n";
if (isUserDefiend)
{
OS << "if(auto casted = godot::Object::cast_to<RLC" << name
<< ">(other)){\n";
OS << "*content = *((*casted).content);\n";
}
else
{
OS << " if (other.get_type() == "
<< godotVariantBuiltinTypesName(type);
OS << "){\n*content = (" << name << "&) other;\n";
}
OS << "return;}\n";
}
OS << "ERR_PRINT(\"could not assign, right hand side of assigment was "
Expand All @@ -1299,6 +1320,8 @@ class RLCLib: public godot::Object {
if (pair.first().starts_with("_") or pair.first() == "main")
continue;
if (llvm::all_of(pair.second, [](mlir::Value v) {
if (v.getDefiningOp<mlir::rlc::ConstantGlobalOp>())
return true;
auto casted = v.getDefiningOp<mlir::rlc::FunctionOp>();
return casted and casted.isDeclaration();
}))
Expand Down Expand Up @@ -1345,6 +1368,8 @@ class RLCLib: public godot::Object {
llvm::SmallVector<mlir::FunctionType, 4> canOverloads;
for (auto overload : pair.second)
{
if (overload.getDefiningOp<mlir::rlc::ConstantGlobalOp>())
continue;
if (auto casted = overload.getDefiningOp<mlir::rlc::FunctionOp>();
casted and
(casted.isInternal() or casted.getUnmangledName() == "main" or
Expand Down
40 changes: 28 additions & 12 deletions lib/dialect/src/Operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,13 @@ static mlir::LogicalResult declareActionTypes(
return mlir::success();

// declares a type that contains the alternative between all possible actions
auto alternative =
mlir::rlc::AlternativeType::get(function.getContext(), declaredTypes, ("Any" + function.getClassType().getName() + "Action").str());
auto alternative = mlir::rlc::AlternativeType::get(
function.getContext(),
declaredTypes,
("Any" + function.getClassType().getName() + "Action").str());

builder.getRewriter().create<mlir::rlc::TypeAliasOp>(
function.getLoc(),
alternative.getName(),
alternative,
nullptr,
nullptr);
function.getLoc(), alternative.getName(), alternative, nullptr, nullptr);

return mlir::success();
}
Expand Down Expand Up @@ -659,9 +657,11 @@ mlir::LogicalResult mlir::rlc::TypeAliasOp::typeCheck(
this->setShugarizedTypeAttr(getShugarizedType()->replaceType(shugarized));
}

if (auto casted = deducedType.dyn_cast<mlir::rlc::AlternativeType>() ) {
deducedType = mlir::rlc::AlternativeType::get(casted.getContext(), casted.getUnderlying(), getName());
}
if (auto casted = deducedType.dyn_cast<mlir::rlc::AlternativeType>())
{
deducedType = mlir::rlc::AlternativeType::get(
casted.getContext(), casted.getUnderlying(), getName());
}

builder.getConverter().registerType(getName(), deducedType);
this->setAliased(deducedType);
Expand Down Expand Up @@ -1353,15 +1353,31 @@ static void promoteArgumentOfIsOp(
return;
}

auto name = table.lookUpValue(isOp.getExpression());
mlir::Value isOperand = isOp.getExpression();
llvm::StringRef name = table.lookUpValue(isOperand);
if (auto casted = mlir::dyn_cast_or_null<mlir::rlc::StorageCast>(
isOperand.getDefiningOp()))
{
name = table.lookUpValue(casted.getOperand());
mlir::IRMapping mapping;
isOperand =
rewriter.clone(*isOperand.getDefiningOp(), mapping)->getResult(0);
}

if (name.empty())
return;

auto upcastedValue = rewriter.create<mlir::rlc::ValueUpcastOp>(
isOp.getLoc(), isOp.getTypeOrTrait(), isOp.getExpression());
isOp.getLoc(), isOp.getTypeOrTrait(), isOperand);
table.add(name, upcastedValue);
}

mlir::LogicalResult mlir::rlc::StorageCast::typeCheck(
mlir::rlc::ModuleBuilder &builder)
{
return mlir::success();
}

mlir::LogicalResult mlir::rlc::TemplateInstantiationOp::typeCheck(
mlir::rlc::ModuleBuilder &builder)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/dialect/src/Operations.td
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,7 @@ def RLC_SelectBranch : RLC_Dialect<"switch", [Terminator, DeclareOpInterfaceMeth
}];
}

def RLC_StorageCast : RLC_Dialect<"storage_cast"> {
def RLC_StorageCast : RLC_Dialect<"storage_cast", [DeclareOpInterfaceMethods<TypeCheckable>]> {
let summary = "Turns context and frame types into a regular type";

let description = [{
Expand Down
2 changes: 1 addition & 1 deletion python/learn.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def main():
load_initial_states(program, args.initial_states)
module_path = os.path.abspath(program.module_path)

ray.init(num_cpus=12, num_gpus=1, include_dashboard=False, log_to_driver=False)
ray.init(num_cpus=12, num_gpus=1, include_dashboard=True, log_to_driver=True)
session_dir = ray.worker._global_node.get_session_dir_path()
print(f"SESSION_DIR: {session_dir}")
from ray import air, tune
Expand Down
14 changes: 10 additions & 4 deletions rlcConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function(rlc TARGET_NAME)
set(options OPTIONAL SHARED COMPILE FUZ)
set(options OPTIONAL SHARED COMPILE FUZ OPTIMIZE DEBUG_INFO)
set(oneValueArgs DESTINATION INPUT OUTPUT HEADER PYTHON GODOT)
set(multiValueArgs EXTRA_ARGS)
cmake_parse_arguments(RLC
Expand All @@ -13,6 +13,12 @@ function(rlc TARGET_NAME)
if (${RLC_COMPILE})
list(APPEND ARG "--compile")
endif()
if (${RLC_OPTIMIZE})
list(APPEND ARG "-O2")
endif()
if (${RLC_DEBUG_INFO})
list(APPEND ARG "-g")
endif()
if (${RLC_SHARED})
list(APPEND ARG "--shared")
endif()
Expand All @@ -37,7 +43,7 @@ function(rlc TARGET_NAME)
set(TMP_GODOT ${CMAKE_CURRENT_BINARY_DIR}/${RLC_GODOT_FILENAME}.tmp)
add_custom_command(
OUTPUT ${RLC_GODOT}
COMMAND rlc ${RLC_INPUT} -o ${TMP_GODOT} -O2 --godot ${RLC_EXTRA_ARGS} -MD
COMMAND rlc ${RLC_INPUT} -o ${TMP_GODOT} --godot ${RLC_EXTRA_ARGS} -MD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${TMP_GODOT} ${RLC_GODOT}
DEPFILE ${RLC_GODOT}.dep
DEPENDS ${RLC_INPUT}
Expand All @@ -53,7 +59,7 @@ function(rlc TARGET_NAME)
set(TMP_HEADER ${CMAKE_CURRENT_BINARY_DIR}/${RLC_HEADER_FILENAME}.tmp)
add_custom_command(
OUTPUT ${RLC_HEADER}
COMMAND rlc ${RLC_INPUT} -o ${TMP_HEADER} -O2 --header ${RLC_EXTRA_ARGS} -MD
COMMAND rlc ${RLC_INPUT} -o ${TMP_HEADER} --header ${RLC_EXTRA_ARGS} -MD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${TMP_HEADER} ${RLC_HEADER}
DEPENDS ${RLC_INPUT}
DEPFILE ${RLC_HEADER}.dep
Expand Down Expand Up @@ -82,7 +88,7 @@ function(rlc TARGET_NAME)
set(RLC_PYTHON_LIB ${RLC_PYTHON_DIR}/lib${CMAKE_SHARED_LIBRARY_SUFFIX})
add_custom_command(
OUTPUT ${RLC_PYTHON_LIB}
COMMAND rlc ${RLC_INPUT} -o ${RLC_PYTHON_LIB} --shared -O2 ${RLC_EXTRA_ARGS} -MD
COMMAND rlc ${RLC_INPUT} -o ${RLC_PYTHON_LIB} --shared ${RLC_EXTRA_ARGS} -MD
DEPENDS ${RLC_INPUT}
DEPFILE ${RLC_PYTHON_LIB}.dep
)
Expand Down
19 changes: 19 additions & 0 deletions tool/rlc/test/frm_ctx_alterantive_upcast.rl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# RUN: rlc %s -o %t -i %stdlib
# RUN: %t%exeext

using Asd = Int | Float

act asd(frm Asd x) -> RollStat:
if x is Int:
x = x + 1
act asd()

fun main() -> Int:
let x : Asd
x = 0
let y = asd(x)
y.asd()
let value = y.x
if value is Int:
return value - 1
return -1

0 comments on commit c70c878

Please sign in to comment.