LLVM 20.0.0git
WebAssemblyAddMissingPrototypes.cpp
Go to the documentation of this file.
1//===-- WebAssemblyAddMissingPrototypes.cpp - Fix prototypeless functions -===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Add prototypes to prototypes-less functions.
11///
12/// WebAssembly has strict function prototype checking so we need functions
13/// declarations to match the call sites. Clang treats prototype-less functions
14/// as varargs (foo(...)) which happens to work on existing platforms but
15/// doesn't under WebAssembly. This pass will find all the call sites of each
16/// prototype-less function, ensure they agree, and then set the signature
17/// on the function declaration accordingly.
18///
19//===----------------------------------------------------------------------===//
20
21#include "WebAssembly.h"
22#include "llvm/IR/Constants.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
25#include "llvm/Pass.h"
26#include "llvm/Support/Debug.h"
29using namespace llvm;
30
31#define DEBUG_TYPE "wasm-add-missing-prototypes"
32
33namespace {
34class WebAssemblyAddMissingPrototypes final : public ModulePass {
35 StringRef getPassName() const override {
36 return "Add prototypes to prototypes-less functions";
37 }
38
39 void getAnalysisUsage(AnalysisUsage &AU) const override {
40 AU.setPreservesCFG();
42 }
43
44 bool runOnModule(Module &M) override;
45
46public:
47 static char ID;
48 WebAssemblyAddMissingPrototypes() : ModulePass(ID) {}
49};
50} // End anonymous namespace
51
52char WebAssemblyAddMissingPrototypes::ID = 0;
53INITIALIZE_PASS(WebAssemblyAddMissingPrototypes, DEBUG_TYPE,
54 "Add prototypes to prototypes-less functions", false, false)
55
57 return new WebAssemblyAddMissingPrototypes();
58}
59
60bool WebAssemblyAddMissingPrototypes::runOnModule(Module &M) {
61 LLVM_DEBUG(dbgs() << "********** Add Missing Prototypes **********\n");
62
63 std::vector<std::pair<Function *, Function *>> Replacements;
64
65 // Find all the prototype-less function declarations
66 for (Function &F : M) {
67 if (!F.isDeclaration() || !F.hasFnAttribute("no-prototype"))
68 continue;
69
70 LLVM_DEBUG(dbgs() << "Found no-prototype function: " << F.getName()
71 << "\n");
72
73 // When clang emits prototype-less C functions it uses (...), i.e. varargs
74 // function that take no arguments (have no sentinel). When we see a
75 // no-prototype attribute we expect the function have these properties.
76 if (!F.isVarArg())
78 "Functions with 'no-prototype' attribute must take varargs: " +
79 F.getName());
80 unsigned NumParams = F.getFunctionType()->getNumParams();
81 if (NumParams != 0) {
82 if (!(NumParams == 1 && F.arg_begin()->hasStructRetAttr()))
83 report_fatal_error("Functions with 'no-prototype' attribute should "
84 "not have params: " +
85 F.getName());
86 }
87
88 // Find calls of this function, looking through bitcasts.
90 SmallVector<Value *> Worklist;
91 Worklist.push_back(&F);
92 while (!Worklist.empty()) {
93 Value *V = Worklist.pop_back_val();
94 for (User *U : V->users()) {
95 if (auto *BC = dyn_cast<BitCastOperator>(U))
96 Worklist.push_back(BC);
97 else if (auto *CB = dyn_cast<CallBase>(U))
98 if (CB->getCalledOperand() == V)
99 Calls.push_back(CB);
100 }
101 }
102
103 // Create a function prototype based on the first call site that we find.
104 FunctionType *NewType = nullptr;
105 for (CallBase *CB : Calls) {
106 LLVM_DEBUG(dbgs() << "prototype-less call of " << F.getName() << ":\n");
107 LLVM_DEBUG(dbgs() << *CB << "\n");
108 FunctionType *DestType = CB->getFunctionType();
109 if (!NewType) {
110 // Create a new function with the correct type
111 NewType = DestType;
112 LLVM_DEBUG(dbgs() << "found function type: " << *NewType << "\n");
113 } else if (NewType != DestType) {
114 errs() << "warning: prototype-less function used with "
115 "conflicting signatures: "
116 << F.getName() << "\n";
117 LLVM_DEBUG(dbgs() << " " << *DestType << "\n");
118 LLVM_DEBUG(dbgs() << " " << *NewType << "\n");
119 }
120 }
121
122 if (!NewType) {
124 dbgs() << "could not derive a function prototype from usage: " +
125 F.getName() + "\n");
126 // We could not derive a type for this function. In this case strip
127 // the isVarArg and make it a simple zero-arg function. This has more
128 // chance of being correct. The current signature of (...) is illegal in
129 // C since it doesn't have any arguments before the "...", we this at
130 // least makes it possible for this symbol to be resolved by the linker.
131 NewType = FunctionType::get(F.getFunctionType()->getReturnType(), false);
132 }
133
134 Function *NewF =
135 Function::Create(NewType, F.getLinkage(), F.getName() + ".fixed_sig");
136 NewF->setAttributes(F.getAttributes());
137 NewF->removeFnAttr("no-prototype");
138 NewF->IsNewDbgInfoFormat = F.IsNewDbgInfoFormat;
139 Replacements.emplace_back(&F, NewF);
140 }
141
142 for (auto &Pair : Replacements) {
143 Function *OldF = Pair.first;
144 Function *NewF = Pair.second;
145 std::string Name = std::string(OldF->getName());
146 M.getFunctionList().push_back(NewF);
147 OldF->replaceAllUsesWith(
149 OldF->eraseFromParent();
150 NewF->setName(Name);
151 }
152
153 return !Replacements.empty();
154}
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define LLVM_DEBUG(...)
Definition: Debug.h:106
std::string Name
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1120
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2268
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:173
bool IsNewDbgInfoFormat
Is this function using intrinsics to record the position of debugging information,...
Definition: Function.h:116
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Function.cpp:458
void removeFnAttr(Attribute::AttrKind Kind)
Remove function attributes from this function.
Definition: Function.cpp:689
void setAttributes(AttributeList Attrs)
Set the attribute list for this Function.
Definition: Function.h:356
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:294
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
bool empty() const
Definition: SmallVector.h:81
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
LLVM Value Representation.
Definition: Value.h:74
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:377
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ModulePass * createWebAssemblyAddMissingPrototypes()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.