Skip to content

Minimize patch #2

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

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
33 changes: 21 additions & 12 deletions clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,18 +332,27 @@ ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
if (OFOrErr) {
auto &OF = OFOrErr.get();
bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
// Find the clang AST section in the container.
for (auto &Section : OF->sections()) {
StringRef Name;
Section.getName(Name);
if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) {
if (Expected<StringRef> E = Section.getContents())
return *E;
else {
handleAllErrors(E.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
EIB.log(llvm::errs());
});
return "";
StringRef SectionName = IsCOFF ? "clangast" : "__clangast";

if (const auto &WasmOF = llvm::unique_dyn_cast<llvm::object::WasmObjectFile>(OF)) {
for (auto &Segment : WasmOF->dataSegments()) {
if (Segment.Data.Name != SectionName) continue;
return llvm::toStringRef(Segment.Data.Content);
}
} else {
// Find the clang AST section in the container.
for (auto &Section : OF->sections()) {
StringRef Name;
Section.getName(Name);
if (Name == SectionName) {
if (Expected<StringRef> E = Section.getContents())
return *E;
else {
handleAllErrors(E.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
EIB.log(llvm::errs());
});
return "";
}
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,6 @@ static void addExceptionArgs(const ArgList &Args, types::ID InputType,
// Disable C++ EH by default on XCore and PS4.
bool CXXExceptionsEnabled =
Triple.getArch() != llvm::Triple::xcore && !Triple.isPS4CPU();
// WebAssembly: no exception handling on WASI
if (Triple.isOSBinFormatWasm())
CXXExceptionsEnabled = false;
Arg *ExceptionArg = Args.getLastArg(
options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
options::OPT_fexceptions, options::OPT_fno_exceptions);
Expand Down
4 changes: 0 additions & 4 deletions llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1682,10 +1682,6 @@ static SectionKind getWasmKindForNamedSection(StringRef Name, SectionKind K) {
if (K.isText())
return SectionKind::getText();

// Clang precompiled header data isn't needed at runtime; use custom section
if (Name == "__clangast")
return SectionKind::getMetadata();

// Otherwise, ignore whatever section type the generic impl detected and use
// a plain data section.
return SectionKind::getData();
Expand Down
63 changes: 4 additions & 59 deletions llvm/lib/MC/WasmObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,7 @@ void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
Section.PayloadOffset = W.OS.tell();

// Custom sections in wasm also have a string identifier.
if (Name != "__clangast") {
writeString(Name);
} else {
// pad section start to nearest 4 bytes for Clang PCH
uint64_t MinLength = Section.PayloadOffset + 5ULL /* min ULEB128 length */ + Name.size();
uint64_t RoundedUpLength = (MinLength + 3ULL) & ~3ULL;
encodeULEB128(Name.size(), W.OS, 5 + (RoundedUpLength - MinLength));
W.OS << Name;
}
writeString(Name);

// The position where the custom section starts.
Section.ContentsOffset = W.OS.tell();
Expand Down Expand Up @@ -493,9 +485,6 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,

if (SymA && SymA->isVariable()) {
const MCExpr *Expr = SymA->getVariableValue();
if (Expr->getKind() != MCExpr::SymbolRef) {
fprintf(stderr, "The expr type is %d\n", (int)Expr->getKind());
}
const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
if (Inner && Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
llvm_unreachable("weakref used in reloc not yet implemented");
Expand Down Expand Up @@ -581,7 +570,6 @@ static const MCSymbolWasm *resolveSymbol(const MCSymbolWasm &Symbol) {
while (Ret->isVariable()) {
const MCExpr *Expr = Ret->getVariableValue();
if (Expr->getKind() == MCExpr::Binary) {
fprintf(stderr, "The expr type is %d\n", (int)Expr->getKind());
Expr = pullSymbol(Expr);
if (!Expr) {
llvm_unreachable("can't find a symbol in this mess\n");
Expand Down Expand Up @@ -1081,16 +1069,8 @@ void WasmObjectWriter::writeCustomSection(WasmCustomSection &CustomSection,
auto *Sec = CustomSection.Section;
startCustomSection(Section, CustomSection.Name);

if (CustomSection.Name == "__clangast") {
// pad to nearest 4 bytes
uint64_t RoundedUp = (Section.ContentsOffset + 3ULL) & ~3ULL;
for (uint64_t Count = 0; Count < RoundedUp - Section.ContentsOffset; Count++) {
W.OS << char(0);
}
}

Sec->setSectionOffset(W.OS.tell() - Section.ContentsOffset);
Asm.writeSectionData(W.OS, Sec, Layout);
Sec->setSectionOffset(W.OS.tell() - Section.ContentsOffset);
Asm.writeSectionData(W.OS, Sec, Layout);

CustomSection.OutputContentsOffset = Section.ContentsOffset;
CustomSection.OutputIndex = Section.Index;
Expand Down Expand Up @@ -1171,29 +1151,9 @@ static bool isInSymtab(const MCSymbolWasm &Sym) {
if (Sym.isSection())
return false;

// Clang's precompiled headers are in a separate custom section
if (Sym.getName() == "__clang_ast")
return false;

return true;
}

// SwiftWasm: takes a MCSymbolWasm that's an alias expression of the form
// ((targetSymbol + constantA) - constantB) + constantC...)
// return the final offset from targetSymbol.
// if no offset, returns 0.
static int64_t getAliasedSymbolOffset(const MCSymbolWasm &Symbol,
const MCAsmLayout &Layout) {
if (!Symbol.isVariable())
return 0;
const MCExpr *Expr = Symbol.getVariableValue();
MCValue Res;
if (!Expr->evaluateAsRelocatable(Res, &Layout, nullptr)) {
report_fatal_error("Can't evaluate alias symbol expression");
}
return Res.getConstant();
}

uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
const MCAsmLayout &Layout) {
uint64_t StartOffset = W.OS.tell();
Expand Down Expand Up @@ -1424,10 +1384,6 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
LLVM_DEBUG(dbgs() << " -> function index: " << Index << "\n");

} else if (WS.isData()) {
if (WS.getName() == "__clang_ast")
continue;
if (WS.isTemporary() && !WS.getSize())
continue;
if (!isInSymtab(WS))
continue;

Expand Down Expand Up @@ -1503,9 +1459,6 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
// Find the target symbol of this weak alias and export that index
const auto &WS = static_cast<const MCSymbolWasm &>(S);
const MCSymbolWasm *ResolvedSym = resolveSymbol(WS);
if (!ResolvedSym) {
continue;
}
LLVM_DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym
<< "'\n");

Expand All @@ -1517,16 +1470,8 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
LLVM_DEBUG(dbgs() << " -> index:" << WasmIndex << "\n");
} else if (ResolvedSym->isData()) {
assert(DataLocations.count(ResolvedSym) > 0);
// SwiftWasm: hack: grab the offset
// Swift has aliases of the form
// alias = ((symbol + constant) - constant)
// so we need to evaluate the constants here using MCExpr
// there's probably a proper way to do this.
int64_t Offset = getAliasedSymbolOffset(WS, Layout);
wasm::WasmDataReference Ref =
const wasm::WasmDataReference &Ref =
DataLocations.find(ResolvedSym)->second;
Ref.Offset += Offset;
Ref.Size -= Offset;
DataLocations[&WS] = Ref;
LLVM_DEBUG(dbgs() << " -> index:" << Ref.Segment << "\n");
} else {
Expand Down