summaryrefslogtreecommitdiff
path: root/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2017-12-18 20:10:56 +0000
committerDimitry Andric <dim@FreeBSD.org>2017-12-18 20:10:56 +0000
commit044eb2f6afba375a914ac9d8024f8f5142bb912e (patch)
tree1475247dc9f9fe5be155ebd4c9069c75aadf8c20 /lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp
parenteb70dddbd77e120e5d490bd8fbe7ff3f8fa81c6b (diff)
Notes
Diffstat (limited to 'lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp')
-rw-r--r--lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp75
1 files changed, 69 insertions, 6 deletions
diff --git a/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp b/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp
index 76a2ff3f9803..666337acccce 100644
--- a/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp
+++ b/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp
@@ -24,6 +24,7 @@
//===----------------------------------------------------------------------===//
#include "WebAssembly.h"
+#include "llvm/IR/CallSite.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
@@ -35,6 +36,11 @@ using namespace llvm;
#define DEBUG_TYPE "wasm-fix-function-bitcasts"
+static cl::opt<bool> TemporaryWorkarounds(
+ "wasm-temporary-workarounds",
+ cl::desc("Apply certain temporary workarounds"),
+ cl::init(true), cl::Hidden);
+
namespace {
class FixFunctionBitcasts final : public ModulePass {
StringRef getPassName() const override {
@@ -68,10 +74,19 @@ static void FindUses(Value *V, Function &F,
if (BitCastOperator *BC = dyn_cast<BitCastOperator>(U.getUser()))
FindUses(BC, F, Uses, ConstantBCs);
else if (U.get()->getType() != F.getType()) {
+ CallSite CS(U.getUser());
+ if (!CS)
+ // Skip uses that aren't immediately called
+ continue;
+ Value *Callee = CS.getCalledValue();
+ if (Callee != V)
+ // Skip calls where the function isn't the callee
+ continue;
if (isa<Constant>(U.get())) {
// Only add constant bitcasts to the list once; they get RAUW'd
auto c = ConstantBCs.insert(cast<Constant>(U.get()));
- if (!c.second) continue;
+ if (!c.second)
+ continue;
}
Uses.push_back(std::make_pair(&U, &F));
}
@@ -97,9 +112,10 @@ static Function *CreateWrapper(Function *F, FunctionType *Ty) {
// Determine what arguments to pass.
SmallVector<Value *, 4> Args;
Function::arg_iterator AI = Wrapper->arg_begin();
+ Function::arg_iterator AE = Wrapper->arg_end();
FunctionType::param_iterator PI = F->getFunctionType()->param_begin();
FunctionType::param_iterator PE = F->getFunctionType()->param_end();
- for (; AI != Wrapper->arg_end() && PI != PE; ++AI, ++PI) {
+ for (; AI != AE && PI != PE; ++AI, ++PI) {
if (AI->getType() != *PI) {
Wrapper->eraseFromParent();
return nullptr;
@@ -108,6 +124,9 @@ static Function *CreateWrapper(Function *F, FunctionType *Ty) {
}
for (; PI != PE; ++PI)
Args.push_back(UndefValue::get(*PI));
+ if (F->isVarArg())
+ for (; AI != AE; ++AI)
+ Args.push_back(&*AI);
CallInst *Call = CallInst::Create(F, Args, "", BB);
@@ -128,11 +147,41 @@ static Function *CreateWrapper(Function *F, FunctionType *Ty) {
}
bool FixFunctionBitcasts::runOnModule(Module &M) {
+ Function *Main = nullptr;
+ CallInst *CallMain = nullptr;
SmallVector<std::pair<Use *, Function *>, 0> Uses;
SmallPtrSet<Constant *, 2> ConstantBCs;
// Collect all the places that need wrappers.
- for (Function &F : M) FindUses(&F, F, Uses, ConstantBCs);
+ for (Function &F : M) {
+ FindUses(&F, F, Uses, ConstantBCs);
+
+ // 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 (!TemporaryWorkarounds && !F.isDeclaration() && F.getName() == "main") {
+ Main = &F;
+ LLVMContext &C = M.getContext();
+ Type *MainArgTys[] = {
+ PointerType::get(Type::getInt8PtrTy(C), 0),
+ Type::getInt32Ty(C)
+ };
+ FunctionType *MainTy = FunctionType::get(Type::getInt32Ty(C), MainArgTys,
+ /*isVarArg=*/false);
+ if (F.getFunctionType() != MainTy) {
+ Value *Args[] = {
+ UndefValue::get(MainArgTys[0]),
+ UndefValue::get(MainArgTys[1])
+ };
+ Value *Casted = ConstantExpr::getBitCast(Main,
+ PointerType::get(MainTy, 0));
+ CallMain = CallInst::Create(Casted, Args, "call_main");
+ Use *UseMain = &CallMain->getOperandUse(2);
+ Uses.push_back(std::make_pair(UseMain, &F));
+ }
+ }
+ }
DenseMap<std::pair<Function *, FunctionType *>, Function *> Wrappers;
@@ -148,9 +197,9 @@ bool FixFunctionBitcasts::runOnModule(Module &M) {
if (!Ty)
continue;
- // Wasm varargs are not ABI-compatible with non-varargs. Just ignore
- // such casts for now.
- if (Ty->isVarArg() || F->isVarArg())
+ // Bitcasted vararg functions occur in Emscripten's implementation of
+ // EM_ASM, so suppress wrappers for them for now.
+ if (TemporaryWorkarounds && (Ty->isVarArg() || F->isVarArg()))
continue;
auto Pair = Wrappers.insert(std::make_pair(std::make_pair(F, Ty), nullptr));
@@ -167,5 +216,19 @@ bool FixFunctionBitcasts::runOnModule(Module &M) {
U->set(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");
+ Function *MainWrapper =
+ cast<Function>(CallMain->getCalledValue()->stripPointerCasts());
+ MainWrapper->setName("main");
+ MainWrapper->setLinkage(Main->getLinkage());
+ MainWrapper->setVisibility(Main->getVisibility());
+ Main->setLinkage(Function::PrivateLinkage);
+ Main->setVisibility(Function::DefaultVisibility);
+ delete CallMain;
+ }
+
return true;
}