diff --git a/clang/lib/AST/Mangle.cpp b/clang/lib/AST/Mangle.cpp --- a/clang/lib/AST/Mangle.cpp +++ b/clang/lib/AST/Mangle.cpp @@ -70,9 +70,7 @@ // On wasm, the argc/argv form of "main" is renamed so that the startup code // can call it with the correct function signature. - // On Emscripten, users may be exporting "main" and expecting to call it - // themselves, so we can't mangle it. - if (Triple.isWasm() && !Triple.isOSEmscripten()) + if (Triple.isWasm()) if (const FunctionDecl *FD = dyn_cast(ND)) if (FD->isMain() && FD->hasPrototype() && FD->param_size() == 2) return CCM_WasmMainArgcArgv; diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -555,10 +555,8 @@ CodeGenFunction(*this).EmitCfiCheckStub(); } emitAtAvailableLinkGuard(); - if (Context.getTargetInfo().getTriple().isWasm() && - !Context.getTargetInfo().getTriple().isOSEmscripten()) { + if (Context.getTargetInfo().getTriple().isWasm()) EmitMainVoidAlias(); - } if (getTriple().isAMDGPU()) { // Emit reference of __amdgpu_device_library_preserve_asan_functions to @@ -6357,8 +6355,10 @@ // new-style no-argument main is in used. if (llvm::Function *F = getModule().getFunction("main")) { if (!F->isDeclaration() && F->arg_size() == 0 && !F->isVarArg() && - F->getReturnType()->isIntegerTy(Context.getTargetInfo().getIntWidth())) - addUsedGlobal(llvm::GlobalAlias::create("__main_void", F)); + F->getReturnType()->isIntegerTy(Context.getTargetInfo().getIntWidth())) { + auto *GA = llvm::GlobalAlias::create("__main_void", F); + GA->setVisibility(llvm::GlobalValue::DefaultVisibility); + } } } diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp --- a/llvm/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp @@ -214,22 +214,9 @@ return Wrapper; } -// Test whether a main function with type FuncTy should be rewritten to have -// type MainTy. -static bool shouldFixMainFunction(FunctionType *FuncTy, FunctionType *MainTy) { - // Only fix the main function if it's the standard zero-arg form. That way, - // the standard cases will work as expected, and users will see signature - // mismatches from the linker for non-standard cases. - return FuncTy->getReturnType() == MainTy->getReturnType() && - FuncTy->getNumParams() == 0 && - !FuncTy->isVarArg(); -} - bool FixFunctionBitcasts::runOnModule(Module &M) { LLVM_DEBUG(dbgs() << "********** Fix Function Bitcasts **********\n"); - Function *Main = nullptr; - CallInst *CallMain = nullptr; SmallVector, 0> Uses; // Collect all the places that need wrappers. @@ -239,29 +226,6 @@ if (F.getCallingConv() == CallingConv::Swift) continue; findUses(&F, F, Uses); - - // If we have a "main" function, and its type isn't - // "int main(int argc, char *argv[])", create an artificial call with it - // bitcasted to that type so that we generate a wrapper for it, so that - // the C runtime can call it. - if (F.getName() == "main") { - Main = &F; - LLVMContext &C = M.getContext(); - Type *MainArgTys[] = {Type::getInt32Ty(C), - PointerType::get(Type::getInt8PtrTy(C), 0)}; - FunctionType *MainTy = FunctionType::get(Type::getInt32Ty(C), MainArgTys, - /*isVarArg=*/false); - if (shouldFixMainFunction(F.getFunctionType(), MainTy)) { - LLVM_DEBUG(dbgs() << "Found `main` function with incorrect type: " - << *F.getFunctionType() << "\n"); - Value *Args[] = {UndefValue::get(MainArgTys[0]), - UndefValue::get(MainArgTys[1])}; - Value *Casted = - ConstantExpr::getBitCast(Main, PointerType::get(MainTy, 0)); - CallMain = CallInst::Create(MainTy, Casted, Args, "call_main"); - Uses.push_back(std::make_pair(CallMain, &F)); - } - } } DenseMap, Function *> Wrappers; @@ -282,25 +246,5 @@ CB->setCalledOperand(Wrapper); } - // If we created a wrapper for main, rename the wrapper so that it's the - // one that gets called from startup. - if (CallMain) { - Main->setName("__original_main"); - auto *MainWrapper = - cast(CallMain->getCalledOperand()->stripPointerCasts()); - delete CallMain; - if (Main->isDeclaration()) { - // The wrapper is not needed in this case as we don't need to export - // it to anyone else. - MainWrapper->eraseFromParent(); - } else { - // Otherwise give the wrapper the same linkage as the original main - // function, so that it can be called from the same places. - MainWrapper->setName("main"); - MainWrapper->setLinkage(Main->getLinkage()); - MainWrapper->setVisibility(Main->getVisibility()); - } - } - return true; }