Compare commits

...

1 Commits

Author SHA1 Message Date
Shilei Tian
9cfb51ae02 [OpenMP] Add the initial support for libomptarget on macOS.
This patch enables building `libomptarget` on macOS. Other components required
for target offloading, such as device runtime, are not enabled yet.
2023-05-01 19:02:12 -04:00
20 changed files with 234 additions and 12 deletions

View File

@@ -249,6 +249,7 @@ static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) {
case Triple::Wasm:
case Triple::DXContainer:
case Triple::SPIRV:
case Triple::MetalLib:
case Triple::UnknownObjectFormat:
break;
}

View File

@@ -5077,6 +5077,7 @@ std::string CGObjCCommonMac::GetSectionName(StringRef Section,
case llvm::Triple::Wasm:
case llvm::Triple::GOFF:
case llvm::Triple::SPIRV:
case llvm::Triple::MetalLib:
case llvm::Triple::XCOFF:
case llvm::Triple::DXContainer:
llvm::report_fatal_error(

View File

@@ -5806,6 +5806,7 @@ CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
case llvm::Triple::GOFF:
case llvm::Triple::SPIRV:
case llvm::Triple::XCOFF:
case llvm::Triple::MetalLib:
llvm_unreachable("unimplemented");
case llvm::Triple::COFF:
case llvm::Triple::ELF:

View File

@@ -54,6 +54,7 @@ class MCSectionELF;
class MCSectionGOFF;
class MCSectionMachO;
class MCSectionSPIRV;
class MCSectionMetalLib;
class MCSectionWasm;
class MCSectionXCOFF;
class MCStreamer;
@@ -87,7 +88,8 @@ public:
IsSPIRV,
IsWasm,
IsXCOFF,
IsDXContainer
IsDXContainer,
IsMetalLib
};
private:
@@ -135,6 +137,7 @@ private:
SpecificBumpPtrAllocator<MCSectionMachO> MachOAllocator;
SpecificBumpPtrAllocator<MCSectionGOFF> GOFFAllocator;
SpecificBumpPtrAllocator<MCSectionSPIRV> SPIRVAllocator;
SpecificBumpPtrAllocator<MCSectionSPIRV> MetalLibAllocator;
SpecificBumpPtrAllocator<MCSectionWasm> WasmAllocator;
SpecificBumpPtrAllocator<MCSectionXCOFF> XCOFFAllocator;
SpecificBumpPtrAllocator<MCInst> MCInstAllocator;
@@ -641,6 +644,8 @@ public:
MCSectionSPIRV *getSPIRVSection();
MCSectionMetalLib *getMetalLibSection();
MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
unsigned Flags = 0) {
return getWasmSection(Section, K, Flags, nullptr);

View File

@@ -0,0 +1,48 @@
//===- MCMetalLibStreamer.h - MetalLib Object File Interface ------*- C++ ===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Overrides MCObjectStreamer to disable all unnecessary features with stubs.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_MC_MCMetalLibSTREAMER_H
#define LLVM_MC_MCMetalLibSTREAMER_H
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCObjectStreamer.h"
#include "llvm/MC/MCObjectWriter.h"
namespace llvm {
class MCInst;
class raw_ostream;
class MCMetalLibStreamer : public MCObjectStreamer {
public:
MCMetalLibStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
std::unique_ptr<MCObjectWriter> OW,
std::unique_ptr<MCCodeEmitter> Emitter)
: MCObjectStreamer(Context, std::move(TAB), std::move(OW),
std::move(Emitter)) {}
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
return false;
}
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Align ByteAlignment) override {}
void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
uint64_t Size = 0, Align ByteAlignment = Align(1),
SMLoc Loc = SMLoc()) override {}
private:
void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &) override;
};
} // end namespace llvm
#endif

View File

@@ -459,6 +459,7 @@ private:
void initGOFFMCObjectFileInfo(const Triple &T);
void initCOFFMCObjectFileInfo(const Triple &T);
void initSPIRVMCObjectFileInfo(const Triple &T);
void initMetalLibMCObjectFileInfo(const Triple &T);
void initWasmMCObjectFileInfo(const Triple &T);
void initXCOFFMCObjectFileInfo(const Triple &T);
void initDXContainerObjectFileInfo(const Triple &T);

View File

@@ -49,6 +49,7 @@ public:
SV_XCOFF,
SV_SPIRV,
SV_DXContainer,
SV_MetalLib,
};
/// Express the state of bundle locked groups while emitting code.

View File

@@ -0,0 +1,41 @@
//===- MCSectionMetalLib.h - MetalLib Machine Code Sections -----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file declares the MCSectionMetalLib class.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_MC_MCSECTIONMetalLib_H
#define LLVM_MC_MCSECTIONMetalLib_H
#include "llvm/MC/MCSection.h"
#include "llvm/MC/SectionKind.h"
namespace llvm {
class MCSymbol;
class MCSectionMetalLib final : public MCSection {
friend class MCContext;
MCSectionMetalLib(SectionKind K, MCSymbol *Begin)
: MCSection(SV_MetalLib, "", K, Begin) {}
// TODO: Add StringRef Name to MCSectionMetalLib.
public:
~MCSectionMetalLib() = default;
void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
raw_ostream &OS,
const MCExpr *Subsection) const override {}
bool useCodeAlign() const override { return false; }
bool isVirtualSection() const override { return false; }
};
} // end namespace llvm
#endif // LLVM_MC_MCSECTIONMetalLib_H

View File

@@ -120,6 +120,11 @@ MCStreamer *createDXContainerStreamer(MCContext &Ctx,
std::unique_ptr<MCObjectWriter> &&OW,
std::unique_ptr<MCCodeEmitter> &&CE,
bool RelaxAll);
MCStreamer *createMetalLibStreamer(MCContext &Ctx,
std::unique_ptr<MCAsmBackend> &&TAB,
std::unique_ptr<MCObjectWriter> &&OW,
std::unique_ptr<MCCodeEmitter> &&CE,
bool RelaxAll);
MCRelocationInfo *createMCRelocationInfo(const Triple &TT, MCContext &Ctx);
@@ -220,13 +225,19 @@ public:
std::unique_ptr<MCAsmBackend> &&TAB,
std::unique_ptr<MCObjectWriter> &&OW,
std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
using DXContainerStreamerCtorTy =
MCStreamer *(*)(const Triple &T, MCContext &Ctx,
std::unique_ptr<MCAsmBackend> &&TAB,
std::unique_ptr<MCObjectWriter> &&OW,
std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
using MetalLibStreamerCtorTy =
MCStreamer *(*)(const Triple &T, MCContext &Ctx,
std::unique_ptr<MCAsmBackend> &&TAB,
std::unique_ptr<MCObjectWriter> &&OW,
std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
using NullTargetStreamerCtorTy = MCTargetStreamer *(*)(MCStreamer &S);
using AsmTargetStreamerCtorTy = MCTargetStreamer *(*)(
MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint,
@@ -333,6 +344,7 @@ private:
XCOFFStreamerCtorTy XCOFFStreamerCtorFn = nullptr;
SPIRVStreamerCtorTy SPIRVStreamerCtorFn = nullptr;
DXContainerStreamerCtorTy DXContainerStreamerCtorFn = nullptr;
MetalLibStreamerCtorTy MetalLibStreamerCtorFn = nullptr;
/// Construction function for this target's null TargetStreamer, if
/// registered (default = nullptr).
@@ -621,6 +633,13 @@ public:
S = createDXContainerStreamer(Ctx, std::move(TAB), std::move(OW),
std::move(Emitter), RelaxAll);
break;
case Triple::MetalLib:
if (MetalLibStreamerCtorFn)
S = MetalLibStreamerCtorFn(T, Ctx, std::move(TAB), std::move(OW),
std::move(Emitter), RelaxAll);
else
S = createMetalLibStreamer(Ctx, std::move(TAB), std::move(OW),
std::move(Emitter), RelaxAll);
}
if (ObjectTargetStreamerCtorFn)
ObjectTargetStreamerCtorFn(*S, STI);

View File

@@ -106,7 +106,9 @@ public:
renderscript32, // 32-bit RenderScript
renderscript64, // 64-bit RenderScript
ve, // NEC SX-Aurora Vector Engine
LastArchType = ve
air, // 32-bit Apple IR
air64, // 64-bit Apple IR
LastArchType = air64
};
enum SubArchType {
NoSubArch,
@@ -288,6 +290,7 @@ public:
SPIRV,
Wasm,
XCOFF,
MetalLib,
};
private:

View File

@@ -5106,6 +5106,9 @@ static const char *getSectionNameForBitcode(const Triple &T) {
case Triple::DXContainer:
llvm_unreachable("DXContainer is not yet implemented");
break;
case Triple::MetalLib:
llvm_unreachable("MetalLib is not yet implemented");
break;
}
llvm_unreachable("Unimplemented ObjectFormatType");
}
@@ -5131,6 +5134,9 @@ static const char *getSectionNameForCommandline(const Triple &T) {
case Triple::DXContainer:
llvm_unreachable("DXC is not yet implemented");
break;
case Triple::MetalLib:
llvm_unreachable("Metal is not yet implemented");
break;
}
llvm_unreachable("Unimplemented ObjectFormatType");
}

View File

@@ -48,6 +48,7 @@ add_llvm_component_library(LLVMMC
MCSectionXCOFF.cpp
MCStreamer.cpp
MCSPIRVStreamer.cpp
MCMetalLibStreamer.cpp
MCSubtargetInfo.cpp
MCSymbol.cpp
MCSymbolELF.cpp

View File

@@ -30,6 +30,7 @@
#include "llvm/MC/MCSectionGOFF.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCSectionSPIRV.h"
#include "llvm/MC/MCSectionMetalLib.h"
#include "llvm/MC/MCSectionWasm.h"
#include "llvm/MC/MCSectionXCOFF.h"
#include "llvm/MC/MCStreamer.h"
@@ -109,6 +110,9 @@ MCContext::MCContext(const Triple &TheTriple, const MCAsmInfo *mai,
case Triple::SPIRV:
Env = IsSPIRV;
break;
case Triple::MetalLib:
Env = IsMetalLib;
break;
case Triple::UnknownObjectFormat:
report_fatal_error("Cannot initialize MC for unknown object file format.");
break;
@@ -253,6 +257,7 @@ MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
case MCContext::IsXCOFF:
return createXCOFFSymbolImpl(Name, IsTemporary);
case MCContext::IsMetalLib:
case MCContext::IsDXContainer:
break;
case MCContext::IsSPIRV:
@@ -852,6 +857,21 @@ MCSectionSPIRV *MCContext::getSPIRVSection() {
return Result;
}
MCSectionMetalLib *MCContext::getMetalLibSection() {
MCSymbol *Begin = nullptr;
MCSectionMetalLib *Result = new (MetalLibAllocator.Allocate())
MCSectionMetalLib(SectionKind::getText(), Begin);
auto *F = new MCDataFragment();
Result->getFragmentList().insert(Result->begin(), F);
F->setParent(Result);
if (Begin)
Begin->setFragment(F);
return Result;
}
MCSectionDXContainer *MCContext::getDXContainerSection(StringRef Section,
SectionKind K) {
// Do the lookup, if we have a hit, return it.

View File

@@ -0,0 +1,44 @@
//===- lib/MC/MCMetalLibStreamer.cpp - MetalLib Object Output -*- C++ -*---===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file assembles .s files and emits MetalLib .o object files.
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCMetalLibStreamer.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/TargetRegistry.h"
using namespace llvm;
void MCMetalLibStreamer::emitInstToData(const MCInst &Inst,
const MCSubtargetInfo &STI) {
MCAssembler &Assembler = getAssembler();
SmallVector<MCFixup, 0> Fixups;
SmallString<256> Code;
Assembler.getEmitter().encodeInstruction(Inst, Code, Fixups, STI);
// Append the encoded instruction to the current data fragment (or create a
// new such fragment if the current fragment is not a data fragment).
MCDataFragment *DF = getOrCreateDataFragment();
DF->setHasInstructions(STI);
DF->getContents().append(Code.begin(), Code.end());
}
MCStreamer *llvm::createMetalLibStreamer(MCContext &Context,
std::unique_ptr<MCAsmBackend> &&MAB,
std::unique_ptr<MCObjectWriter> &&OW,
std::unique_ptr<MCCodeEmitter> &&CE,
bool RelaxAll) {
MCMetalLibStreamer *S = new MCMetalLibStreamer(Context, std::move(MAB),
std::move(OW), std::move(CE));
if (RelaxAll)
S->getAssembler().setRelaxAll(true);
return S;
}

View File

@@ -829,6 +829,11 @@ void MCObjectFileInfo::initSPIRVMCObjectFileInfo(const Triple &T) {
TextSection = Ctx->getSPIRVSection();
}
void MCObjectFileInfo::initMetalLibMCObjectFileInfo(const Triple &T) {
// Put everything in a single binary section.
TextSection = Ctx->getSPIRVSection();
}
void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) {
TextSection = Ctx->getWasmSection(".text", SectionKind::getText());
DataSection = Ctx->getWasmSection(".data", SectionKind::getData());
@@ -1079,6 +1084,9 @@ void MCObjectFileInfo::initMCObjectFileInfo(MCContext &MCCtx, bool PIC,
case MCContext::IsDXContainer:
initDXContainerObjectFileInfo(TheTriple);
break;
case MCContext::IsMetalLib:
initMetalLibMCObjectFileInfo(TheTriple);
break;
}
}
@@ -1097,6 +1105,7 @@ MCSection *MCObjectFileInfo::getDwarfComdatSection(const char *Name,
case Triple::SPIRV:
case Triple::XCOFF:
case Triple::DXContainer:
case Triple::MetalLib:
case Triple::UnknownObjectFormat:
report_fatal_error("Cannot get DWARF comdat section for this object file "
"format: not implemented.");

View File

@@ -800,6 +800,10 @@ AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
report_fatal_error(
"Need to implement createSPIRVAsmParser for SPIRV format.");
break;
case MCContext::IsMetalLib:
report_fatal_error(
"Need to implement createMetalLibAsmParser for SPIRV format.");
break;
case MCContext::IsWasm:
PlatformParser.reset(createWasmAsmParser());
break;

View File

@@ -84,6 +84,8 @@ StringRef Triple::getArchTypeName(ArchType Kind) {
case x86_64: return "x86_64";
case xcore: return "xcore";
case xtensa: return "xtensa";
case air: return "air";
case air64: return "air64";
}
llvm_unreachable("Invalid ArchType!");
@@ -308,6 +310,7 @@ StringRef Triple::getObjectFormatTypeName(ObjectFormatType Kind) {
case XCOFF: return "xcoff";
case DXContainer: return "dxcontainer";
case SPIRV: return "spirv";
case MetalLib: return "metallib";
}
llvm_unreachable("unknown object format type");
}
@@ -873,6 +876,9 @@ static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
case Triple::dxil:
return Triple::DXContainer;
case Triple::air:
case Triple::air64:
return Triple::MetalLib;
}
llvm_unreachable("unknown architecture");
}
@@ -1431,6 +1437,7 @@ static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
case llvm::Triple::x86:
case llvm::Triple::xcore:
case llvm::Triple::xtensa:
case llvm::Triple::air:
return 32;
case llvm::Triple::aarch64:
@@ -1456,6 +1463,7 @@ static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
case llvm::Triple::ve:
case llvm::Triple::wasm64:
case llvm::Triple::x86_64:
case llvm::Triple::air64:
return 64;
}
llvm_unreachable("Invalid architecture value");
@@ -1522,6 +1530,7 @@ Triple Triple::get32BitArchVariant() const {
case Triple::x86:
case Triple::xcore:
case Triple::xtensa:
case Triple::air:
// Already 32-bit.
break;
@@ -1549,6 +1558,7 @@ Triple Triple::get32BitArchVariant() const {
break;
case Triple::wasm64: T.setArch(Triple::wasm32); break;
case Triple::x86_64: T.setArch(Triple::x86); break;
case Triple::air64: T.setArch(Triple::air); break;
}
return T;
}
@@ -1599,6 +1609,7 @@ Triple Triple::get64BitArchVariant() const {
case Triple::ve:
case Triple::wasm64:
case Triple::x86_64:
case Triple::air64:
// Already 64-bit.
break;
@@ -1629,6 +1640,7 @@ Triple Triple::get64BitArchVariant() const {
case Triple::thumbeb: T.setArch(Triple::aarch64_be); break;
case Triple::wasm32: T.setArch(Triple::wasm64); break;
case Triple::x86: T.setArch(Triple::x86_64); break;
case Triple::air: T.setArch(Triple::air64); break;
}
return T;
}

View File

@@ -1920,6 +1920,7 @@ StringRef ModuleAddressSanitizer::getGlobalMetadataSection() const {
case Triple::SPIRV:
case Triple::XCOFF:
case Triple::DXContainer:
case Triple::MetalLib:
report_fatal_error(
"ModuleAddressSanitizer not implemented for object file format");
case Triple::UnknownObjectFormat:

View File

@@ -71,11 +71,11 @@ set(OPENMP_TEST_OPENMP_FLAGS ${OPENMP_TEST_COMPILER_OPENMP_FLAGS} CACHE STRING
"OpenMP compiler flag to use for testing OpenMP runtime libraries.")
set(ENABLE_LIBOMPTARGET ON)
# Currently libomptarget cannot be compiled on Windows or MacOS X.
# Since the device plugins are only supported on Linux anyway,
# there is no point in trying to compile libomptarget on other OSes.
# Currently libomptarget cannot be compiled on Windows.
# Since the device plugins are only supported on Linux anyway, there is no point
# in trying to compile libomptarget on other OSes.
# 32-bit systems are not supported either.
if (APPLE OR WIN32 OR NOT OPENMP_HAVE_STD_CPP17_FLAG OR NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
if (WIN32 OR NOT OPENMP_HAVE_STD_CPP17_FLAG OR NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
set(ENABLE_LIBOMPTARGET OFF)
endif()
@@ -97,8 +97,8 @@ add_subdirectory(runtime)
if (OPENMP_ENABLE_LIBOMPTARGET)
# Check that the library can actually be built.
if (APPLE OR WIN32)
message(FATAL_ERROR "libomptarget cannot be built on Windows and MacOS X!")
if (WIN32)
message(FATAL_ERROR "libomptarget cannot be built on Windows!")
elseif (NOT OPENMP_HAVE_STD_CPP17_FLAG)
message(FATAL_ERROR "Host compiler must support C++17 to build libomptarget!")
elseif (NOT CMAKE_SIZEOF_VOID_P EQUAL 8)

View File

@@ -105,9 +105,13 @@ set(LIBOMPTARGET_LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE STRING
"Path to folder containing llvm library libomptarget.so")
# Build offloading plugins and device RTLs if they are available.
add_subdirectory(plugins)
add_subdirectory(plugins-nextgen)
add_subdirectory(DeviceRTL)
# FIXME: For now let's disable building other stuff on macOS.
if (NOT APPLE)
add_subdirectory(plugins)
add_subdirectory(plugins-nextgen)
add_subdirectory(DeviceRTL)
endif()
add_subdirectory(tools)
# Add tests.