From 34cdf7ec43c7363a5aa5d1db7e1db7e3a691a696 Mon Sep 17 00:00:00 2001 From: hsmhsm Date: Mon, 6 Jan 2020 21:55:52 +0530 Subject: [PATCH 1/2] minimal necessary code changes required for registering a new backend to llvm so that llc can recognize it --- llvm/CMakeLists.txt | 1 + llvm/include/llvm/ADT/Triple.h | 1 + llvm/include/llvm/BinaryFormat/ELF.h | 2 + llvm/lib/Support/Triple.cpp | 10 +++++ llvm/lib/Target/LLVMBuild.txt | 1 + llvm/lib/Target/Toy/CMakeLists.txt | 6 +++ llvm/lib/Target/Toy/LLVMBuild.txt | 31 +++++++++++++ .../Target/Toy/MCTargetDesc/CMakeLists.txt | 3 ++ .../lib/Target/Toy/MCTargetDesc/LLVMBuild.txt | 22 ++++++++++ .../Toy/MCTargetDesc/ToyMCTargetDesc.cpp | 20 +++++++++ llvm/lib/Target/Toy/TargetInfo/CMakeLists.txt | 3 ++ llvm/lib/Target/Toy/TargetInfo/LLVMBuild.txt | 22 ++++++++++ .../Target/Toy/TargetInfo/ToyTargetInfo.cpp | 21 +++++++++ .../lib/Target/Toy/TargetInfo/ToyTargetInfo.h | 20 +++++++++ llvm/lib/Target/Toy/ToyTargetMachine.cpp | 43 +++++++++++++++++++ llvm/lib/Target/Toy/ToyTargetMachine.h | 32 ++++++++++++++ 16 files changed, 238 insertions(+) create mode 100644 llvm/lib/Target/Toy/CMakeLists.txt create mode 100644 llvm/lib/Target/Toy/LLVMBuild.txt create mode 100644 llvm/lib/Target/Toy/MCTargetDesc/CMakeLists.txt create mode 100644 llvm/lib/Target/Toy/MCTargetDesc/LLVMBuild.txt create mode 100644 llvm/lib/Target/Toy/MCTargetDesc/ToyMCTargetDesc.cpp create mode 100644 llvm/lib/Target/Toy/TargetInfo/CMakeLists.txt create mode 100644 llvm/lib/Target/Toy/TargetInfo/LLVMBuild.txt create mode 100644 llvm/lib/Target/Toy/TargetInfo/ToyTargetInfo.cpp create mode 100644 llvm/lib/Target/Toy/TargetInfo/ToyTargetInfo.h create mode 100644 llvm/lib/Target/Toy/ToyTargetMachine.cpp create mode 100644 llvm/lib/Target/Toy/ToyTargetMachine.h diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt index 9bbcba069c1d8..10e30858b0983 100644 --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -299,6 +299,7 @@ set(LLVM_ALL_TARGETS RISCV Sparc SystemZ + Toy WebAssembly X86 XCore diff --git a/llvm/include/llvm/ADT/Triple.h b/llvm/include/llvm/ADT/Triple.h index b9485e81feb73..81c3c56d0da27 100644 --- a/llvm/include/llvm/ADT/Triple.h +++ b/llvm/include/llvm/ADT/Triple.h @@ -91,6 +91,7 @@ class Triple { kalimba, // Kalimba: generic kalimba shave, // SHAVE: Movidius vector VLIW processors lanai, // Lanai: Lanai 32-bit + toy, // TOY: Experiment with LLVM backend architecture wasm32, // WebAssembly with 32-bit pointers wasm64, // WebAssembly with 64-bit pointers renderscript32, // 32-bit RenderScript diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h index caab91da9c839..1248d7e4edc36 100644 --- a/llvm/include/llvm/BinaryFormat/ELF.h +++ b/llvm/include/llvm/BinaryFormat/ELF.h @@ -311,6 +311,8 @@ enum { EM_RISCV = 243, // RISC-V EM_LANAI = 244, // Lanai 32-bit processor EM_BPF = 247, // Linux kernel bpf virtual machine + + EM_TOY = 347, // Experiment with LLVM backend architecture }; // Object file classes. diff --git a/llvm/lib/Support/Triple.cpp b/llvm/lib/Support/Triple.cpp index d419463e6a5e6..e52f3bf1c3bf8 100644 --- a/llvm/lib/Support/Triple.cpp +++ b/llvm/lib/Support/Triple.cpp @@ -65,6 +65,7 @@ StringRef Triple::getArchTypeName(ArchType Kind) { case spir64: return "spir64"; case kalimba: return "kalimba"; case lanai: return "lanai"; + case toy: return "toy"; case shave: return "shave"; case wasm32: return "wasm32"; case wasm64: return "wasm64"; @@ -138,6 +139,7 @@ StringRef Triple::getArchTypePrefix(ArchType Kind) { case spir64: return "spir"; case kalimba: return "kalimba"; case lanai: return "lanai"; + case toy: return "toy"; case shave: return "shave"; case wasm32: case wasm64: return "wasm"; @@ -310,6 +312,7 @@ Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) { .Case("spir64", spir64) .Case("kalimba", kalimba) .Case("lanai", lanai) + .Case("toy", toy) .Case("shave", shave) .Case("wasm32", wasm32) .Case("wasm64", wasm64) @@ -438,6 +441,7 @@ static Triple::ArchType parseArch(StringRef ArchName) { .Case("spir64", Triple::spir64) .StartsWith("kalimba", Triple::kalimba) .Case("lanai", Triple::lanai) + .Case("toy", Triple::toy) .Case("shave", Triple::shave) .Case("wasm32", Triple::wasm32) .Case("wasm64", Triple::wasm64) @@ -701,6 +705,7 @@ static Triple::ObjectFormatType getDefaultFormat(const Triple &T) { case Triple::tcele: case Triple::thumbeb: case Triple::xcore: + case Triple::toy: return Triple::ELF; case Triple::ppc: @@ -1262,6 +1267,7 @@ static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) { case llvm::Triple::shave: case llvm::Triple::wasm32: case llvm::Triple::renderscript32: + case llvm::Triple::toy: return 32; case llvm::Triple::aarch64: @@ -1343,6 +1349,7 @@ Triple Triple::get32BitArchVariant() const { case Triple::shave: case Triple::wasm32: case Triple::renderscript32: + case Triple::toy: // Already 32-bit. break; @@ -1381,6 +1388,7 @@ Triple Triple::get64BitArchVariant() const { case Triple::xcore: case Triple::sparcel: case Triple::shave: + case Triple::toy: T.setArch(UnknownArch); break; @@ -1467,6 +1475,7 @@ Triple Triple::getBigEndianArchVariant() const { // drop any arch suffixes. case Triple::arm: case Triple::thumb: + case Triple::toy: T.setArch(UnknownArch); break; @@ -1553,6 +1562,7 @@ bool Triple::isLittleEndian() const { case Triple::tcele: case Triple::renderscript32: case Triple::renderscript64: + case Triple::toy: return true; default: return false; diff --git a/llvm/lib/Target/LLVMBuild.txt b/llvm/lib/Target/LLVMBuild.txt index d6a95a3c67133..1651c7751ddaf 100644 --- a/llvm/lib/Target/LLVMBuild.txt +++ b/llvm/lib/Target/LLVMBuild.txt @@ -33,6 +33,7 @@ subdirectories = RISCV Sparc SystemZ + Toy WebAssembly X86 XCore diff --git a/llvm/lib/Target/Toy/CMakeLists.txt b/llvm/lib/Target/Toy/CMakeLists.txt new file mode 100644 index 0000000000000..fd91a097b4552 --- /dev/null +++ b/llvm/lib/Target/Toy/CMakeLists.txt @@ -0,0 +1,6 @@ +add_llvm_target(ToyCodeGen + ToyTargetMachine.cpp + ) + +add_subdirectory(MCTargetDesc) +add_subdirectory(TargetInfo) diff --git a/llvm/lib/Target/Toy/LLVMBuild.txt b/llvm/lib/Target/Toy/LLVMBuild.txt new file mode 100644 index 0000000000000..5acebbb5bab62 --- /dev/null +++ b/llvm/lib/Target/Toy/LLVMBuild.txt @@ -0,0 +1,31 @@ +;===- ./lib/Target/Toy/LLVMBuild.txt ---------------------------*- Conf -*--===; +; +; 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 is an LLVMBuild description file for the components in this subdirectory. +; +; For more information on the LLVMBuild system, please see: +; +; http://llvm.org/docs/LLVMBuild.html +; +;===------------------------------------------------------------------------===; + +[common] +subdirectories = MCTargetDesc TargetInfo + +[component_0] +type = TargetGroup +name = Toy +parent = Target + +[component_1] +type = Library +name = ToyCodeGen +parent = Toy +required_libraries = AsmPrinter CodeGen Core MC SelectionDAG + ToyDesc ToyInfo Support Target +add_to_library_groups = Toy diff --git a/llvm/lib/Target/Toy/MCTargetDesc/CMakeLists.txt b/llvm/lib/Target/Toy/MCTargetDesc/CMakeLists.txt new file mode 100644 index 0000000000000..c91319c5d99e8 --- /dev/null +++ b/llvm/lib/Target/Toy/MCTargetDesc/CMakeLists.txt @@ -0,0 +1,3 @@ +add_llvm_component_library(LLVMToyDesc + ToyMCTargetDesc.cpp + ) diff --git a/llvm/lib/Target/Toy/MCTargetDesc/LLVMBuild.txt b/llvm/lib/Target/Toy/MCTargetDesc/LLVMBuild.txt new file mode 100644 index 0000000000000..526c60c8bdb93 --- /dev/null +++ b/llvm/lib/Target/Toy/MCTargetDesc/LLVMBuild.txt @@ -0,0 +1,22 @@ +;===- ./lib/Target/Toy/MCTargetDesc/LLVMBuild.txt --------------*- Conf -*--===; +; +; 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 is an LLVMBuild description file for the components in this subdirectory. +; +; For more information on the LLVMBuild system, please see: +; +; http://llvm.org/docs/LLVMBuild.html +; +;===------------------------------------------------------------------------===; + +[component_0] +type = Library +name = ToyDesc +parent = Toy +required_libraries = MC ToyInfo Support +add_to_library_groups = Toy diff --git a/llvm/lib/Target/Toy/MCTargetDesc/ToyMCTargetDesc.cpp b/llvm/lib/Target/Toy/MCTargetDesc/ToyMCTargetDesc.cpp new file mode 100644 index 0000000000000..4b6428425c39d --- /dev/null +++ b/llvm/lib/Target/Toy/MCTargetDesc/ToyMCTargetDesc.cpp @@ -0,0 +1,20 @@ +//===-- ToyMCTargetDesc.cpp - Toy Target Descriptions ---------------------===// +// +// 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 provides Toy specific target descriptions. +// +//===----------------------------------------------------------------------===// + +#include "TargetInfo/ToyTargetInfo.h" +using namespace llvm; + +extern "C" void LLVMInitializeToyTargetMC() { + // TODO: We need this stub function definition during target registration, + // otherwise we get an undefined symbol error. Appropriately fill-up this stub + // function later. +} diff --git a/llvm/lib/Target/Toy/TargetInfo/CMakeLists.txt b/llvm/lib/Target/Toy/TargetInfo/CMakeLists.txt new file mode 100644 index 0000000000000..2699ee8e1e12f --- /dev/null +++ b/llvm/lib/Target/Toy/TargetInfo/CMakeLists.txt @@ -0,0 +1,3 @@ +add_llvm_component_library(LLVMToyInfo + ToyTargetInfo.cpp + ) diff --git a/llvm/lib/Target/Toy/TargetInfo/LLVMBuild.txt b/llvm/lib/Target/Toy/TargetInfo/LLVMBuild.txt new file mode 100644 index 0000000000000..9f247df10076c --- /dev/null +++ b/llvm/lib/Target/Toy/TargetInfo/LLVMBuild.txt @@ -0,0 +1,22 @@ +;===- ./lib/Target/Toy/TargetInfo/LLVMBuild.txt ----------------*- Conf -*--===; +; +; 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 is an LLVMBuild description file for the components in this subdirectory. +; +; For more information on the LLVMBuild system, please see: +; +; http://llvm.org/docs/LLVMBuild.html +; +;===------------------------------------------------------------------------===; + +[component_0] +type = Library +name = ToyInfo +parent = Toy +required_libraries = Support +add_to_library_groups = Toy diff --git a/llvm/lib/Target/Toy/TargetInfo/ToyTargetInfo.cpp b/llvm/lib/Target/Toy/TargetInfo/ToyTargetInfo.cpp new file mode 100644 index 0000000000000..d07e21c659ab2 --- /dev/null +++ b/llvm/lib/Target/Toy/TargetInfo/ToyTargetInfo.cpp @@ -0,0 +1,21 @@ +//===-- ToyTargetInfo.cpp - Toy Target Implementation ---------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "TargetInfo/ToyTargetInfo.h" +#include "llvm/Support/TargetRegistry.h" +using namespace llvm; + +Target &llvm::getTheToyTarget() { + static Target TheToyTarget; + return TheToyTarget; +} + +extern "C" void LLVMInitializeToyTargetInfo() { + RegisterTarget X(getTheToyTarget(), "toy", + "Toy", "Toy"); +} diff --git a/llvm/lib/Target/Toy/TargetInfo/ToyTargetInfo.h b/llvm/lib/Target/Toy/TargetInfo/ToyTargetInfo.h new file mode 100644 index 0000000000000..9fbf6ef05ac33 --- /dev/null +++ b/llvm/lib/Target/Toy/TargetInfo/ToyTargetInfo.h @@ -0,0 +1,20 @@ +//===-- ToyTargetInfo.h - Toy Target Implementation -------------*- 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIB_TARGET_TOY_TARGETINFO_TOYTARGETINFO_H +#define LLVM_LIB_TARGET_TOY_TARGETINFO_TOYTARGETINFO_H + +namespace llvm { + +class Target; + +Target &getTheToyTarget(); + +} // namespace llvm + +#endif // LLVM_LIB_TARGET_TOY_TARGETINFO_TOYTARGETINFO_H diff --git a/llvm/lib/Target/Toy/ToyTargetMachine.cpp b/llvm/lib/Target/Toy/ToyTargetMachine.cpp new file mode 100644 index 0000000000000..5fee6edd93726 --- /dev/null +++ b/llvm/lib/Target/Toy/ToyTargetMachine.cpp @@ -0,0 +1,43 @@ +//===-- ToyTargetMachine.cpp - Define TargetMachine for Toy ---------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// +//===----------------------------------------------------------------------===// + +#include "ToyTargetMachine.h" +#include "TargetInfo/ToyTargetInfo.h" +#include "llvm/Support/TargetRegistry.h" +using namespace llvm; + +extern "C" void LLVMInitializeToyTarget() { + // Register the target. + RegisterTargetMachine X(getTheToyTarget()); +} + +static std::string computeDataLayout() { + return ""; +} + +static Reloc::Model getEffectiveRelocModel(Optional RM) { + return *RM; +} + +static CodeModel::Model getEffectiveToyCodeModel() { + return CodeModel::Small; +} + +ToyTargetMachine::ToyTargetMachine( + const Target &T, const Triple &TT, StringRef &CPU, StringRef &FS, + const TargetOptions &Options, Optional &RM, + Optional &CM, CodeGenOpt::Level &OL, bool &JIT) + : LLVMTargetMachine(T, computeDataLayout(), TT, CPU, FS, Options, + getEffectiveRelocModel(RM), + getEffectiveToyCodeModel(), OL) { +} + +ToyTargetMachine::~ToyTargetMachine() {} diff --git a/llvm/lib/Target/Toy/ToyTargetMachine.h b/llvm/lib/Target/Toy/ToyTargetMachine.h new file mode 100644 index 0000000000000..2907e32b99856 --- /dev/null +++ b/llvm/lib/Target/Toy/ToyTargetMachine.h @@ -0,0 +1,32 @@ +//===-- ToyTargetMachine.h - Define TargetMachine for Toy -------*- 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 Toy specific subclass of TargetMachine. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIB_TARGET_TOY_TOYTARGETMACHINE_H +#define LLVM_LIB_TARGET_TOY_TOYTARGETMACHINE_H + +#include "llvm/Target/TargetMachine.h" + +namespace llvm { + +class ToyTargetMachine : public LLVMTargetMachine { + +public: + ToyTargetMachine(const Target &T, const Triple &TT, StringRef &CPU, + StringRef &FS, const TargetOptions &Options, + Optional &RM, Optional &CM, + CodeGenOpt::Level &OL, bool &JIT); + ~ToyTargetMachine() override; +}; + +} // end namespace llvm + +#endif From 2706a96a1c39f13352f1a9535fc57bfb96fe0738 Mon Sep 17 00:00:00 2001 From: hsmhsm Date: Mon, 6 Jan 2020 23:04:21 +0530 Subject: [PATCH 2/2] added a few comment lines for stub functions and classes. --- llvm/lib/Target/Toy/ToyTargetMachine.cpp | 4 ++++ llvm/lib/Target/Toy/ToyTargetMachine.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/llvm/lib/Target/Toy/ToyTargetMachine.cpp b/llvm/lib/Target/Toy/ToyTargetMachine.cpp index 5fee6edd93726..670f62cfb4c85 100644 --- a/llvm/lib/Target/Toy/ToyTargetMachine.cpp +++ b/llvm/lib/Target/Toy/ToyTargetMachine.cpp @@ -20,18 +20,22 @@ extern "C" void LLVMInitializeToyTarget() { } static std::string computeDataLayout() { + // TODO: Appropriately fill-up this stub function later return ""; } static Reloc::Model getEffectiveRelocModel(Optional RM) { + // TODO: Appropriately fill-up this stub function later return *RM; } static CodeModel::Model getEffectiveToyCodeModel() { + // TODO: Appropriately fill-up this stub function later return CodeModel::Small; } ToyTargetMachine::ToyTargetMachine( + // TODO: Appropriately fill-up this stub constructor later const Target &T, const Triple &TT, StringRef &CPU, StringRef &FS, const TargetOptions &Options, Optional &RM, Optional &CM, CodeGenOpt::Level &OL, bool &JIT) diff --git a/llvm/lib/Target/Toy/ToyTargetMachine.h b/llvm/lib/Target/Toy/ToyTargetMachine.h index 2907e32b99856..6e3224d1f90ab 100644 --- a/llvm/lib/Target/Toy/ToyTargetMachine.h +++ b/llvm/lib/Target/Toy/ToyTargetMachine.h @@ -17,6 +17,7 @@ namespace llvm { +// TODO: Appropriately define this stub class later class ToyTargetMachine : public LLVMTargetMachine { public: @@ -24,6 +25,7 @@ class ToyTargetMachine : public LLVMTargetMachine { StringRef &FS, const TargetOptions &Options, Optional &RM, Optional &CM, CodeGenOpt::Level &OL, bool &JIT); + ~ToyTargetMachine() override; };