diff --git a/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp b/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp --- a/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp +++ b/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp @@ -24,8 +24,9 @@ std::vector> ArgVStorage; std::vector ArgV; - ArgVStorage.reserve(Args.size() + (ProgramName ? 1 : 0)); - ArgV.reserve(Args.size() + 1 + (ProgramName ? 1 : 0)); + int ArgC = Args.size() + (ProgramName ? 1 : 0); + ArgVStorage.reserve(ArgC); + ArgV.reserve(ArgC + 1); if (ProgramName) { ArgVStorage.push_back(std::make_unique(ProgramName->size() + 1)); @@ -42,7 +43,7 @@ } ArgV.push_back(nullptr); - return Main(Args.size(), ArgV.data()); + return Main(ArgC, ArgV.data()); } CtorDtorIterator::CtorDtorIterator(const GlobalVariable *GV, bool End) diff --git a/llvm/test/ExecutionEngine/OrcLazy/runAsMain0.ll b/llvm/test/ExecutionEngine/OrcLazy/runAsMain0.ll new file mode 100644 --- /dev/null +++ b/llvm/test/ExecutionEngine/OrcLazy/runAsMain0.ll @@ -0,0 +1,7 @@ +; RUN: lli -jit-kind=orc-lazy %s -o output input +; +; Test that passing no arguments is ok +; +define i32 @main() { + ret i32 0 +} diff --git a/llvm/test/ExecutionEngine/OrcLazy/runAsMain2.ll b/llvm/test/ExecutionEngine/OrcLazy/runAsMain2.ll new file mode 100644 --- /dev/null +++ b/llvm/test/ExecutionEngine/OrcLazy/runAsMain2.ll @@ -0,0 +1,32 @@ +; RUN: lli -jit-kind=orc-lazy %s -o output input | FileCheck %s +; +; Test that trailing arguments are passed to JITed main function, +; if it takes two arguments. +; +; CHECK: {{.*}}runAsMain2.ll +; CHECK-NEXT: -o +; CHECK-NEXT: output +; CHECK-NEXT: input +; +define i32 @main(i32 %argc, i8** nocapture readonly %argv) { + %1 = icmp sgt i32 %argc, 0 + br i1 %1, label %2, label %4 + +2: + %3 = zext i32 %argc to i64 + br label %5 + +4: + ret i32 0 + +5: + %6 = phi i64 [ 0, %2 ], [ %10, %5 ] + %7 = getelementptr inbounds i8*, i8** %argv, i64 %6 + %8 = load i8*, i8** %7 + %9 = call i32 @puts(i8* nonnull dereferenceable(1) %8) + %10 = add nuw nsw i64 %6, 1 + %11 = icmp eq i64 %10, %3 + br i1 %11, label %4, label %5 +} + +declare i32 @puts(i8* nocapture readonly) diff --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp --- a/llvm/tools/lli/lli.cpp +++ b/llvm/tools/lli/lli.cpp @@ -854,12 +854,6 @@ ExitOnErr(J->addObjectFile(std::move(Obj))); } - // Generate a argument string. - std::vector Args; - Args.push_back(InputFile); - for (auto &Arg : InputArgv) - Args.push_back(Arg); - // Run any static constructors. ExitOnErr(J->runConstructors()); @@ -878,8 +872,8 @@ typedef int (*MainFnPtr)(int, char *[]); auto Result = orc::runAsMain( - jitTargetAddressToFunction(MainSym.getAddress()), Args, - StringRef("lli")); + jitTargetAddressToFunction(MainSym.getAddress()), InputArgv, + StringRef(InputFile)); // Wait for -entry-point threads. for (auto &AltEntryThread : AltEntryThreads)