Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove support for linear memory in the cheerp-genericjs target #232

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions clang/lib/CodeGen/CGException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,14 @@ llvm::BasicBlock *CodeGenFunction::EmitLandingPad() {
LPadInst = Builder.CreateLandingPad(LPadTy, 0);

llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0);
if(!CGM.getTarget().isByteAddressable()) {
if (CGM.getTarget().getTriple().isCheerpWasm()) {
LPadExn = Builder.CreateIntToPtr(LPadExn, Int8PtrTy);
} else {
llvm::Function *MakeReg = CGM.getIntrinsic(llvm::Intrinsic::cheerp_make_regular, {Int8PtrTy, Int8PtrTy});
LPadExn = Builder.CreateCall(MakeReg, {llvm::ConstantPointerNull::get(Int8PtrTy), LPadExn});
}
}
Builder.CreateStore(LPadExn, getExceptionSlot());
llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1);
Builder.CreateStore(LPadSel, getEHSelectorSlot());
Expand Down Expand Up @@ -1575,6 +1583,14 @@ llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() {
llvm::Value *Exn = nullptr;
if (getLangOpts().CPlusPlus) {
Exn = Builder.CreateExtractValue(LPadInst, 0);
if(!CGM.getTarget().isByteAddressable()) {
if (CGM.getTarget().getTriple().isCheerpWasm()) {
Exn = Builder.CreateIntToPtr(Exn, Int8PtrTy);
} else {
llvm::Function *MakeReg = CGM.getIntrinsic(llvm::Intrinsic::cheerp_make_regular, {Int8PtrTy, Int8PtrTy});
Exn = Builder.CreateCall(MakeReg, {llvm::ConstantPointerNull::get(Int8PtrTy), Exn});
}
}
}
llvm::CallInst *terminateCall =
CGM.getCXXABI().emitTerminateForUnexpectedException(*this, Exn);
Expand Down Expand Up @@ -1675,6 +1691,14 @@ llvm::BasicBlock *CodeGenFunction::getEHResumeBlock(bool isCleanup) {

llvm::Type *LPadType = GetLandingPadTy();
llvm::Value *LPadVal = llvm::UndefValue::get(LPadType);
if (!CGM.getTarget().isByteAddressable()) {
if (CGM.getTarget().getTriple().isCheerpWasm()) {
Exn = Builder.CreatePtrToInt(Exn, Int32Ty);
} else {
llvm::Function *PtrOffset = CGM.getIntrinsic(llvm::Intrinsic::cheerp_pointer_offset, {Int8PtrTy});
Exn = Builder.CreateCall(PtrOffset, Exn);
}
}
LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val");
LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val");
Builder.CreateResume(LPadVal);
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/CodeGen/CodeGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2016,9 +2016,10 @@ class CodeGenFunction : public CodeGenTypeCache {
auto* Ret = llvm::StructType::getTypeByName(CGM.getLLVMContext(), "struct._ZN10__cxxabiv119__cheerp_landingpadE");
if (Ret)
return Ret;
llvm::Type* Tys[] { CGM.Int8PtrTy, CGM.Int32Ty};
llvm::Type* Tys[] { CGM.Int32Ty, CGM.Int32Ty};

return llvm::StructType::create(Tys, "struct._ZN10__cxxabiv119__cheerp_landingpadE", false, nullptr, false, true /*asmjs*/);
bool asmjs = getTarget().getTriple().isCheerpWasm();
return llvm::StructType::create(Tys, "struct._ZN10__cxxabiv119__cheerp_landingpadE", false, nullptr, false, asmjs);
}

/// Returns the contents of the function's exception object and selector
Expand Down
14 changes: 7 additions & 7 deletions libcxxabi/src/cxa_cheerp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ terminate() noexcept
namespace [[cheerp::genericjs]] __cxxabiv1 {

struct
#ifdef __ASMJS__
[[cheerp::wasm]]
#endif
__cheerp_landingpad
{
void* val;
uintptr_t val;
int sel;

[[cheerp::genericjs]]
static void set_val(__cheerp_landingpad* lp, void* v) noexcept
void set_val(void* v) noexcept
{
int intval = __builtin_cheerp_pointer_offset(v);
__cheerp_landingpad** hack = reinterpret_cast<__cheerp_landingpad**>(lp);
*hack = reinterpret_cast<__cheerp_landingpad*>(intval);
val = __builtin_cheerp_pointer_offset(v);
}
};

Expand Down Expand Up @@ -537,15 +537,15 @@ __gxx_personality_v0
}
}
}
__cheerp_landingpad lp{nullptr, 0};
__cheerp_landingpad lp{0, 0};
if(!native)
{
curNonNativeException = obj;
return lp;
}

Exception* ex = current_exception;
__cheerp_landingpad::set_val(&lp, ex);
lp.set_val(ex);

for(int i = start; i < start+n; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/ADT/Triple.h
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ class Triple {

/// Tests wheter the target is cheerp-wasm
bool isCheerpWasm() const {
return getArch() == Triple::cheerp && getObjectFormat() == Triple::Wasm;
return getArch() == Triple::cheerp && getEnvironment() == Triple::WebAssembly;
}

/// Tests whether the target supports the EHABI exception
Expand Down
24 changes: 4 additions & 20 deletions llvm/include/llvm/Cheerp/GlobalDepsAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,10 @@ class GlobalDepsAnalyzer
*/
bool needCreatePointerArray() const { return hasPointerArrays; }

/**
* Determine if we need to compile the asm.js module
*/
bool needAsmJSCode() const { return hasAsmJSCode; }
/**
* Determine if we need to compile the asm.js module
*/
bool needAsmJSMemory() const { return hasAsmJSMemory; }
/**
* Determine if we need to compile the definition of CheerpException
*/
bool needCheerpException() const { return hasCheerpException; }

/**
* Determine if linear memory malloc is ever used
*/
bool usesAsmJSMalloc() const { return hasAsmJSMalloc; }

bool usesAtomics() const { return hasAtomics; }

Expand Down Expand Up @@ -241,8 +228,10 @@ class GlobalDepsAnalyzer

static void replaceFunctionAliasWithAliasee(llvm::Module &module, llvm::StringRef name);

//Extend lifetime of function, visiting them and declaring external
void extendLifetime(llvm::Function* F);
//Extend lifetime of global, visiting them and declaring external
void extendLifetime(llvm::GlobalValue* G);
// Same but F might be null
void extendLifetimeIfPresent(llvm::GlobalValue* G);

//Determine whether an instruction is atomic.
bool isAtomicInstruction(const llvm::Instruction& I);
Expand Down Expand Up @@ -274,16 +263,11 @@ class GlobalDepsAnalyzer
bool hasCreateClosureUsers;
bool hasVAArgs;
bool hasPointerArrays;
bool hasAsmJSCode;
bool hasAtomics;
bool hasAsmJSMemory;
bool hasAsmJSMalloc;
bool hasCheerpException;
bool mayNeedAsmJSFree;

bool llcPass;
bool hasUndefinedSymbolErrors;
bool preserveFree;
public:
bool forceTypedArrays;
bool needsBuiltin(BuiltinInstr::BUILTIN b)
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/Cheerp/Writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ifndef _CHEERP_WRITER_H
#define _CHEERP_WRITER_H

#include "llvm/ADT/Triple.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Cheerp/AllocaMerging.h"
#include "llvm/Cheerp/BaseWriter.h"
Expand Down Expand Up @@ -182,6 +183,7 @@ class CheerpWriter final : public CheerpBaseWriter
llvm::ModuleAnalysisManager& MAM;
llvm::FunctionAnalysisManager& FAM;
llvm::DataLayout targetData;
bool isWasmTarget;
const llvm::Function* currentFun;
// function-local map of type_info* to typeid, for exception handling
llvm::DenseMap<llvm::Value*, int> typeIdMap;
Expand Down Expand Up @@ -617,6 +619,7 @@ class CheerpWriter final : public CheerpBaseWriter
MAM(MAM),
FAM(MAM.getResult<llvm::FunctionAnalysisManagerModuleProxy>(m).getManager()),
targetData(&m),
isWasmTarget(llvm::Triple(m.getTargetTriple()).isCheerpWasm()),
currentFun(NULL),
PA(PA),
registerize(registerize),
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/IR/IntrinsicsCheerp.td
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def int_cheerp_pointer_offset : Intrinsic<[llvm_i32_ty],
def int_cheerp_is_linear_heap : Intrinsic<[llvm_i1_ty],
[llvm_anyptr_ty],
[IntrNoMem]>;
def int_cheerp_pointer_elem_size : Intrinsic<[llvm_i32_ty],
[llvm_anyptr_ty],
[IntrNoMem]>;

// Closure creation for callbacks
def int_cheerp_create_closure : Intrinsic<[llvm_anyptr_ty],
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CheerpUtils/AllocaLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ bool AllocaLowering::runOnFunction(Function& F, DominatorTree& DT, cheerp::Globa
nbytes = (nbytes + 7) & -8;

Function *getStack, *setStack;
if (asmjs || !GDA.needAsmJSCode())
if (asmjs)
{
getStack = Intrinsic::getDeclaration(M, Intrinsic::stacksave);
setStack = Intrinsic::getDeclaration(M, Intrinsic::stackrestore);
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/CheerpUtils/CallConstructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ PreservedAnalyses CallConstructorsPass::run(llvm::Module &M, llvm::ModuleAnalysi
}
else
{
Value* EnvA = Builder.CreateAlloca(EnvTy);
Env = Builder.CreateLoad(EnvTy, EnvA);
Env = Builder.CreateAlloca(Builder.getInt8Ty()->getPointerTo(0));
Builder.CreateStore(ConstantPointerNull::get(cast<PointerType>(EnvTy->getPointerElementType())), Env);
}

Expand Down
Loading