llvm.org GIT mirror llvm / 2e99c3a
[WebAssemby] Re-apply r320041: "Support main functions with alternate signatures." This includes a fix so that it doesn't transform declarations, and it puts the functionality under control of a command-line option which is off by default to avoid breaking existing setups. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@320196 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 2 years ago
3 changed file(s) with 87 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
3434 using namespace llvm;
3535
3636 #define DEBUG_TYPE "wasm-fix-function-bitcasts"
37
38 static cl::opt TemporaryWorkarounds(
39 "wasm-temporary-workarounds",
40 cl::desc("Apply certain temporary workarounds"),
41 cl::init(true), cl::Hidden);
3742
3843 namespace {
3944 class FixFunctionBitcasts final : public ModulePass {
141146 }
142147
143148 bool FixFunctionBitcasts::runOnModule(Module &M) {
149 Function *Main = nullptr;
150 CallInst *CallMain = nullptr;
144151 SmallVector, 0> Uses;
145152 SmallPtrSet ConstantBCs;
146153
147154 // Collect all the places that need wrappers.
148 for (Function &F : M) FindUses(&F, F, Uses, ConstantBCs);
155 for (Function &F : M) {
156 FindUses(&F, F, Uses, ConstantBCs);
157
158 // If we have a "main" function, and its type isn't
159 // "int main(int argc, char *argv[])", create an artificial call with it
160 // bitcasted to that type so that we generate a wrapper for it, so that
161 // the C runtime can call it.
162 if (!TemporaryWorkarounds && !F.isDeclaration() && F.getName() == "main") {
163 Main = &F;
164 LLVMContext &C = M.getContext();
165 Type *MainArgTys[] = {
166 PointerType::get(Type::getInt8PtrTy(C), 0),
167 Type::getInt32Ty(C)
168 };
169 FunctionType *MainTy = FunctionType::get(Type::getInt32Ty(C), MainArgTys,
170 /*isVarArg=*/false);
171 if (F.getFunctionType() != MainTy) {
172 Value *Args[] = {
173 UndefValue::get(MainArgTys[0]),
174 UndefValue::get(MainArgTys[1])
175 };
176 Value *Casted = ConstantExpr::getBitCast(Main,
177 PointerType::get(MainTy, 0));
178 CallMain = CallInst::Create(Casted, Args, "call_main");
179 Use *UseMain = &CallMain->getOperandUse(2);
180 Uses.push_back(std::make_pair(UseMain, &F));
181 }
182 }
183 }
149184
150185 DenseMap, Function *> Wrappers;
151186
180215 U->set(Wrapper);
181216 }
182217
218 // If we created a wrapper for main, rename the wrapper so that it's the
219 // one that gets called from startup.
220 if (CallMain) {
221 Main->setName("__original_main");
222 Function *MainWrapper =
223 cast(CallMain->getCalledValue()->stripPointerCasts());
224 MainWrapper->setName("main");
225 MainWrapper->setLinkage(Main->getLinkage());
226 MainWrapper->setVisibility(Main->getVisibility());
227 Main->setLinkage(Function::PrivateLinkage);
228 Main->setVisibility(Function::DefaultVisibility);
229 delete CallMain;
230 }
231
183232 return true;
184233 }
0 ; RUN: llc < %s -asm-verbose=false -wasm-temporary-workarounds=false | FileCheck %s
1
2 ; Test main functions with alternate signatures.
3
4 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
5 target triple = "wasm32-unknown-unknown-wasm"
6
7 declare void @main()
8
9 define void @foo() {
10 call void @main()
11 ret void
12 }
13
14 ; CHECK-NOT: __original_main
15 ; CHECK-LABEL: foo:
16 ; CHECK-NEXT: call main@FUNCTION
17 ; CHECK-NEXT: end_function
18 ; CHECK-NOT: __original_main
0 ; RUN: llc < %s -asm-verbose=false -wasm-temporary-workarounds=false | FileCheck %s
1
2 ; Test main functions with alternate signatures.
3
4 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
5 target triple = "wasm32-unknown-unknown-wasm"
6
7 define void @main() {
8 ret void
9 }
10
11 ; CHECK-LABEL: .L__original_main:
12 ; CHECK-NEXT: end_function
13
14 ; CHECK-LABEL: main:
15 ; CHECK-NEXT: .param i32, i32
16 ; CHECK-NEXT: .result i32
17 ; CHECK: call .L__original_main@FUNCTION