summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLang Hames <lhames@gmail.com>2020-01-11 12:58:38 -0800
committerLang Hames <lhames@gmail.com>2020-01-11 13:03:38 -0800
commit2cdb18afda841392002feafda21af31854c195b3 (patch)
treea3282082b1a69db4838dfdf4333b171dcfbd0a2a
parenta1f16998f371870ca4da8b3c00a093c607a36ddd (diff)
downloadbcm5719-llvm-2cdb18afda841392002feafda21af31854c195b3.tar.gz
bcm5719-llvm-2cdb18afda841392002feafda21af31854c195b3.zip
[ORC] Fix argv handling in runAsMain / lli.
This fixes an off-by-one error in the argc value computed by runAsMain, and switches lli back to using the input bitcode (rather than the string "lli") as the effective program name. Thanks to Stefan Graenitz for spotting the bug.
-rw-r--r--llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp2
-rw-r--r--llvm/test/ExecutionEngine/OrcLazy/printargv.ll81
-rw-r--r--llvm/tools/lli/lli.cpp10
3 files changed, 84 insertions, 9 deletions
diff --git a/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp b/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
index 0a57348dd3d..3d97fe9eeab 100644
--- a/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
@@ -42,7 +42,7 @@ int runAsMain(int (*Main)(int, char *[]), ArrayRef<std::string> Args,
}
ArgV.push_back(nullptr);
- return Main(Args.size(), ArgV.data());
+ return Main(Args.size() + !!ProgramName, ArgV.data());
}
CtorDtorIterator::CtorDtorIterator(const GlobalVariable *GV, bool End)
diff --git a/llvm/test/ExecutionEngine/OrcLazy/printargv.ll b/llvm/test/ExecutionEngine/OrcLazy/printargv.ll
new file mode 100644
index 00000000000..b5153a4a2cd
--- /dev/null
+++ b/llvm/test/ExecutionEngine/OrcLazy/printargv.ll
@@ -0,0 +1,81 @@
+; RUN: lli -jit-kind=orc-lazy %s a b c | FileCheck %s
+
+; CHECK: argc = 4
+; CHECK-NEXT: argv = ["{{.*}}printargv.ll", "a", "b", "c"]
+; CHECK-NEXT; argv[4] = null
+
+@.str = private unnamed_addr constant [11 x i8] c"argc = %i\0A\00", align 1
+@.str.1 = private unnamed_addr constant [9 x i8] c"argv = [\00", align 1
+@.str.3 = private unnamed_addr constant [5 x i8] c"\22%s\22\00", align 1
+@.str.4 = private unnamed_addr constant [5 x i8] c"null\00", align 1
+@.str.5 = private unnamed_addr constant [7 x i8] c", \22%s\22\00", align 1
+@.str.6 = private unnamed_addr constant [15 x i8] c"argv[%i] = %s\0A\00", align 1
+@.str.7 = private unnamed_addr constant [5 x i8] c"junk\00", align 1
+@str.8 = private unnamed_addr constant [2 x i8] c"]\00", align 1
+
+define i32 @main(i32 %argc, i8** nocapture readonly %argv) {
+entry:
+ %call = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %argc)
+ %call1 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
+ %cmp = icmp eq i32 %argc, 0
+ br i1 %cmp, label %if.then, label %if.end
+
+if.then:
+ %puts36 = tail call i32 @puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @str.8, i64 0, i64 0))
+ br label %if.end
+
+if.end:
+ %0 = load i8*, i8** %argv, align 8
+ %tobool = icmp eq i8* %0, null
+ br i1 %tobool, label %if.else, label %if.then3
+
+if.then3:
+ %call5 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %0)
+ br label %if.end7
+
+if.else:
+ %call6 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
+ br label %if.end7
+
+if.end7:
+ %cmp837 = icmp eq i32 %argc, 1
+ br i1 %cmp837, label %for.cond.cleanup, label %for.body.preheader
+
+for.body.preheader:
+ %1 = zext i32 %argc to i64
+ br label %for.body
+
+for.cond.cleanup:
+ %puts = tail call i32 @puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @str.8, i64 0, i64 0))
+ %idxprom19 = sext i32 %argc to i64
+ %arrayidx20 = getelementptr inbounds i8*, i8** %argv, i64 %idxprom19
+ %2 = load i8*, i8** %arrayidx20, align 8
+ %tobool21 = icmp eq i8* %2, null
+ %cond = select i1 %tobool21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0)
+ %call22 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %argc, i8* %cond)
+ ret i32 0
+
+for.body:
+ %indvars.iv = phi i64 [ 1, %for.body.preheader ], [ %indvars.iv.next, %for.inc ]
+ %arrayidx9 = getelementptr inbounds i8*, i8** %argv, i64 %indvars.iv
+ %3 = load i8*, i8** %arrayidx9, align 8
+ %tobool10 = icmp eq i8* %3, null
+ br i1 %tobool10, label %if.else15, label %if.then11
+
+if.then11:
+ %call14 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %3)
+ br label %for.inc
+
+if.else15:
+ %call16 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
+ br label %for.inc
+
+for.inc:
+ %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
+ %cmp8 = icmp eq i64 %indvars.iv.next, %1
+ br i1 %cmp8, label %for.cond.cleanup, label %for.body
+}
+
+declare i32 @printf(i8* nocapture readonly, ...)
+
+declare i32 @puts(i8* nocapture readonly)
diff --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp
index 87a8c9f6932..bfe7e8f0430 100644
--- a/llvm/tools/lli/lli.cpp
+++ b/llvm/tools/lli/lli.cpp
@@ -854,12 +854,6 @@ int runOrcLazyJIT(const char *ProgName) {
ExitOnErr(J->addObjectFile(std::move(Obj)));
}
- // Generate a argument string.
- std::vector<std::string> Args;
- Args.push_back(InputFile);
- for (auto &Arg : InputArgv)
- Args.push_back(Arg);
-
// Run any static constructors.
ExitOnErr(J->runConstructors());
@@ -878,8 +872,8 @@ int runOrcLazyJIT(const char *ProgName) {
typedef int (*MainFnPtr)(int, char *[]);
auto Result = orc::runAsMain(
- jitTargetAddressToFunction<MainFnPtr>(MainSym.getAddress()), Args,
- StringRef("lli"));
+ jitTargetAddressToFunction<MainFnPtr>(MainSym.getAddress()), InputArgv,
+ StringRef(InputFile));
// Wait for -entry-point threads.
for (auto &AltEntryThread : AltEntryThreads)
OpenPOWER on IntegriCloud