Skip to content

Commit f6ff79d

Browse files
committed
Added fearless FFI!
1 parent 3719cfe commit f6ff79d

File tree

15 files changed

+214
-19
lines changed

15 files changed

+214
-19
lines changed

Backend/src/function/expressions/externs.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,26 @@ Ref buildExternCall(
281281
buildFlare(FL(), globalState, functionState, builder, "Return ptr! ", ptrToIntLE(globalState, builder, localPtrLE));
282282
hostArgsLE.insert(hostArgsLE.begin(), localPtrLE);
283283

284-
auto resultLE =
285-
buildSideCall(
286-
globalState, hostReturnRefLT, builder, sideStackI8PtrLE, externFuncL, hostArgsLE);
287-
assert(LLVMTypeOf(resultLE) == LLVMVoidTypeInContext(globalState->context));
288-
284+
if (globalState->opt->enableSideCalling) {
285+
auto resultLE =
286+
buildSideCall(
287+
globalState, LLVMVoidTypeInContext(globalState->context), builder, sideStackI8PtrLE, externFuncL,
288+
hostArgsLE);
289+
assert(LLVMTypeOf(resultLE) == LLVMVoidTypeInContext(globalState->context));
290+
} else {
291+
auto resultLE = buildCall(globalState, builder, externFuncL, hostArgsLE);
292+
assert(LLVMTypeOf(resultLE) == LLVMVoidTypeInContext(globalState->context));
293+
}
289294
hostReturnLE = LLVMBuildLoad(builder, localPtrLE, "hostReturn");
290295
buildFlare(FL(), globalState, functionState, builder, "Loaded the return! ", LLVMABISizeOfType(globalState->dataLayout, LLVMTypeOf(hostReturnLE)));
291296
} else {
292-
hostReturnLE =
293-
buildSideCall(globalState, hostReturnRefLT, builder, sideStackI8PtrLE, externFuncL, hostArgsLE);
297+
if (globalState->opt->enableSideCalling) {
298+
hostReturnLE =
299+
buildSideCall(globalState, hostReturnRefLT, builder, sideStackI8PtrLE, externFuncL, hostArgsLE);
300+
} else {
301+
hostReturnLE =
302+
buildCall(globalState, builder, externFuncL, hostArgsLE);
303+
}
294304
}
295305

296306
buildFlare(FL(), globalState, functionState, builder, "Done calling function ", prototype->name->name);

Backend/src/function/expressions/shared/shared.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ LLVMValueRef buildSideCall(
675675
//buildPrint(globalState, entryBuilder, "\n");
676676
}
677677
} else {
678-
callResultAfterReturnLE = makeVoid(globalState);
678+
callResultAfterReturnLE = LLVMGetUndef(LLVMVoidTypeInContext(globalState->context));
679679
}
680680

681681
//buildPrint(globalState, entryBuilder, "Done!\n");

Backend/src/globalstate.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ IRegion* GlobalState::getRegion(RegionId* regionId) {
116116
return rcImm;
117117
} else if (regionId == metalCache->linearRegionId) {
118118
return linearRegion;
119+
} else if (regionId == metalCache->mutRegionId) {
120+
return mutRegion;
119121
// } else if (regionId == metalCache->unsafeRegionId) {
120122
// return unsafeRegion;
121123
// } else if (regionId == metalCache->assistRegionId) {

Backend/src/mainFunction.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,23 @@ LLVMValueRef makeEntryFunction(
223223
LLVMBuildStore(entryBuilder, numArgsLE, globalState->numMainArgs);
224224
LLVMBuildStore(entryBuilder, argsLE, globalState->mainArgs);
225225

226-
LLVMBuildStore(
227-
entryBuilder,
228-
buildCall(globalState, entryBuilder, globalState->externs->malloc, {
229-
constI64LE(globalState, STACK_SIZE)
230-
}),
231-
globalState->sideStack);
226+
if (globalState->opt->enableSideCalling) {
227+
LLVMBuildStore(
228+
entryBuilder,
229+
buildCall(
230+
globalState, entryBuilder, globalState->externs->malloc,
231+
{ constI64LE(globalState, STACK_SIZE) }),
232+
globalState->sideStack);
233+
}
232234

233235
auto calleeUserFunction = globalState->lookupFunction(valeMainPrototype);
234236
auto resultLE = buildCall(globalState, entryBuilder, calleeUserFunction, {});
235237

236-
buildPrint(globalState, entryBuilder, "Done!\n");
238+
if (globalState->opt->enableSideCalling) {
239+
buildCall(
240+
globalState, entryBuilder, globalState->externs->free,
241+
{ LLVMBuildLoad(entryBuilder, globalState->sideStack, "") });
242+
}
237243

238244
LLVMBuildRet(entryBuilder, resultLE);
239245
LLVMDisposeBuilder(entryBuilder);

Backend/src/vale.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ void compileValeCode(GlobalState* globalState, std::vector<std::string>& inputFi
847847
globalState->mutRegion = new Assist(globalState);
848848
break;
849849
case RegionOverride::NAIVE_RC:
850-
globalState->mutRegion = new NaiveRC(globalState, globalState->metalCache->rcImmRegionId);
850+
globalState->mutRegion = new NaiveRC(globalState, globalState->metalCache->mutRegionId);
851851
break;
852852
case RegionOverride::FAST:
853853
globalState->mutRegion = new Unsafe(globalState);

Backend/src/valeopts.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ enum
4949
OPT_OVERRIDE_KNOWN_LIVE_TRUE,
5050
OPT_PRINT_MEM_OVERHEAD,
5151
OPT_ENABLE_REPLAYING,
52+
OPT_ENABLE_SIDE_CALLING,
5253
OPT_CENSUS,
5354
OPT_REGION_OVERRIDE,
5455
OPT_FILENAMES,
@@ -95,6 +96,7 @@ static opt_arg_t args[] =
9596
{ "override_known_live_true", '\0', OPT_ARG_NONE, OPT_OVERRIDE_KNOWN_LIVE_TRUE },
9697
{ "print_mem_overhead", '\0', OPT_ARG_OPTIONAL, OPT_PRINT_MEM_OVERHEAD },
9798
{ "enable_replaying", '\0', OPT_ARG_OPTIONAL, OPT_ENABLE_REPLAYING },
99+
{ "enable_side_calling", '\0', OPT_ARG_OPTIONAL, OPT_ENABLE_SIDE_CALLING },
98100
{ "census", '\0', OPT_ARG_OPTIONAL, OPT_CENSUS },
99101
{ "region_override", '\0', OPT_ARG_REQUIRED, OPT_REGION_OVERRIDE },
100102
{ "ir", '\0', OPT_ARG_NONE, OPT_IR },
@@ -278,14 +280,25 @@ int valeOptSet(ValeOptions *opt, int *argc, char **argv) {
278280
case OPT_ENABLE_REPLAYING: {
279281
if (!s.arg_val) {
280282
opt->enableReplaying = true;
281-
} else if (s.arg_val == std::string("on")) {
283+
} else if (s.arg_val == std::string("true")) {
282284
opt->enableReplaying = true;
283-
} else if (s.arg_val == std::string("off")) {
285+
} else if (s.arg_val == std::string("false")) {
284286
opt->enableReplaying = false;
285287
} else assert(false);
286288
break;
287289
}
288290

291+
case OPT_ENABLE_SIDE_CALLING: {
292+
if (!s.arg_val) {
293+
opt->enableSideCalling = true;
294+
} else if (s.arg_val == std::string("true")) {
295+
opt->enableSideCalling = true;
296+
} else if (s.arg_val == std::string("false")) {
297+
opt->enableSideCalling = false;
298+
} else assert(false);
299+
break;
300+
}
301+
289302
case OPT_CENSUS: {
290303
if (!s.arg_val) {
291304
opt->census = true;

Backend/src/valeopts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct ValeOptions {
4949
bool overrideKnownLiveTrue = false; // Enables generational heap
5050
bool printMemOverhead = false; // Enables generational heap
5151
bool enableReplaying = false; // Enables deterministic replaying
52+
bool enableSideCalling = false; // Enables side calling, used for fearless FFI
5253

5354
RegionOverride regionOverride = RegionOverride::ASSIST;
5455
};

Backend/test/tethercrash.vale

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import stdlib.*;
33
struct Spaceship {
44
fuel int;
55
}
6-
func main() int export {
6+
exported func main() int {
77
print("Starting...\n");
88
s = Spaceship(73);
99
b = &s;

Coordinator/src/build.vale

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ func build_stuff(compiler_dir &Path, all_args &Array<imm, str>) {
146146
"Whether to enable recording and replaying.",
147147
"false",
148148
"If true, the program will be able to make recordings, such that we can deterministically replay them."),
149+
Flag(
150+
"--enable_side_calling",
151+
FLAG_BOOL(),
152+
"Whether to enable fearless FFI and green threads.",
153+
"false",
154+
"If true, the program will be able to make temporary stacks without firing up threads, saving space and making extern calls a bit safer."),
149155
Flag(
150156
"--run_backend",
151157
FLAG_BOOL(),
@@ -232,6 +238,7 @@ func build_stuff(compiler_dir &Path, all_args &Array<imm, str>) {
232238
run_clang = parsed_flags.get_bool_flag("--run_clang", true);
233239
sanity_check = parsed_flags.get_bool_flag("--sanity_check", true);
234240
enable_replaying = parsed_flags.get_bool_flag("--enable_replaying", false);
241+
enable_side_calling = parsed_flags.get_bool_flag("--enable_side_calling", false);
235242
output_vpst = parsed_flags.get_bool_flag("--output_vpst", true);
236243
no_std = parsed_flags.get_bool_flag("--no_std", false);
237244

@@ -391,6 +398,7 @@ func build_stuff(compiler_dir &Path, all_args &Array<imm, str>) {
391398
verify,
392399
llvm_ir,
393400
enable_replaying,
401+
enable_side_calling,
394402
print_mem_overhead,
395403
elide_checks_for_known_live,
396404
override_known_live_true);

Coordinator/src/midas.vale

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func invoke_backend(
1212
verify bool,
1313
llvm_ir bool,
1414
enable_replaying bool,
15+
enable_side_calling bool,
1516
print_mem_overhead bool,
1617
elide_checks_for_known_live bool,
1718
override_known_live_true bool)
@@ -54,6 +55,9 @@ Subprocess {
5455
if (enable_replaying) {
5556
command_line_args.add("--enable_replaying");
5657
}
58+
if (enable_side_calling) {
59+
command_line_args.add("--enable_side_calling=true");
60+
}
5761
if (print_mem_overhead) {
5862
command_line_args.add("--print_mem_overhead");
5963
}

0 commit comments

Comments
 (0)