[LoongArch 3/6] Add target stub for LoongArch
authorLu Weining <luweining@loongson.cn>
Thu, 10 Feb 2022 09:12:09 +0000 (09:12 +0000)
committerRenato Golin <rengolin@systemcall.eu>
Thu, 10 Feb 2022 10:23:34 +0000 (10:23 +0000)
This patch registers the 'loongarch32' and 'loongarch64' targets. Also adds a
simple testcase to check the output of llc --vesion containing the targets.

Differential revision: https://reviews.llvm.org/D115860

llvm/lib/Target/LoongArch/CMakeLists.txt [new file with mode: 0644]
llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp [new file with mode: 0644]
llvm/lib/Target/LoongArch/LoongArchTargetMachine.h [new file with mode: 0644]
llvm/lib/Target/LoongArch/MCTargetDesc/CMakeLists.txt [new file with mode: 0644]
llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCTargetDesc.cpp [new file with mode: 0644]
llvm/lib/Target/LoongArch/TargetInfo/CMakeLists.txt [new file with mode: 0644]
llvm/lib/Target/LoongArch/TargetInfo/LoongArchTargetInfo.cpp [new file with mode: 0644]
llvm/lib/Target/LoongArch/TargetInfo/LoongArchTargetInfo.h [new file with mode: 0644]
llvm/test/CodeGen/LoongArch/lit.local.cfg [new file with mode: 0644]
llvm/test/CodeGen/LoongArch/target_support.ll [new file with mode: 0644]

diff --git a/llvm/lib/Target/LoongArch/CMakeLists.txt b/llvm/lib/Target/LoongArch/CMakeLists.txt
new file mode 100644 (file)
index 0000000..254320f
--- /dev/null
@@ -0,0 +1,24 @@
+add_llvm_component_group(LoongArch)
+
+add_llvm_target(LoongArchCodeGen
+  LoongArchTargetMachine.cpp
+
+  LINK_COMPONENTS
+  Analysis
+  AsmPrinter
+  CodeGen
+  Core
+  MC
+  LoongArchDesc
+  LoongArchInfo
+  SelectionDAG
+  Support
+  Target
+  GlobalISel
+
+  ADD_TO_COMPONENT
+  LoongArch
+  )
+
+add_subdirectory(MCTargetDesc)
+add_subdirectory(TargetInfo)
diff --git a/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp b/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp
new file mode 100644 (file)
index 0000000..66507b4
--- /dev/null
@@ -0,0 +1,52 @@
+//===-- LoongArchTargetMachine.cpp - Define TargetMachine for LoongArch ---===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Implements the info about LoongArch target spec.
+//
+//===----------------------------------------------------------------------===//
+
+#include "LoongArchTargetMachine.h"
+#include "TargetInfo/LoongArchTargetInfo.h"
+#include "llvm/MC/TargetRegistry.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "loongarch"
+
+extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchTarget() {
+  // Register the target.
+  RegisterTargetMachine<LoongArchTargetMachine> X(getTheLoongArch32Target());
+  RegisterTargetMachine<LoongArchTargetMachine> Y(getTheLoongArch64Target());
+}
+
+// FIXME: This is just a placeholder to make current commit buildable. Body of
+// this function will be filled in later commits.
+static std::string computeDataLayout(const Triple &TT) {
+  std::string Ret;
+  Ret += "e";
+  return Ret;
+}
+
+static Reloc::Model getEffectiveRelocModel(const Triple &TT,
+                                           Optional<Reloc::Model> RM) {
+  if (!RM.hasValue())
+    return Reloc::Static;
+  return *RM;
+}
+
+LoongArchTargetMachine::LoongArchTargetMachine(
+    const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
+    const TargetOptions &Options, Optional<Reloc::Model> RM,
+    Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
+    : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
+                        getEffectiveRelocModel(TT, RM),
+                        getEffectiveCodeModel(CM, CodeModel::Small), OL) {
+  initAsmInfo();
+}
+
+LoongArchTargetMachine::~LoongArchTargetMachine() = default;
diff --git a/llvm/lib/Target/LoongArch/LoongArchTargetMachine.h b/llvm/lib/Target/LoongArch/LoongArchTargetMachine.h
new file mode 100644 (file)
index 0000000..31c78ee
--- /dev/null
@@ -0,0 +1,33 @@
+//=- LoongArchTargetMachine.h - Define TargetMachine for LoongArch -*- 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 LoongArch specific subclass of TargetMachine.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_LOONGARCH_LOONGARCHTARGETMACHINE_H
+#define LLVM_LIB_TARGET_LOONGARCH_LOONGARCHTARGETMACHINE_H
+
+#include "llvm/Target/TargetMachine.h"
+
+namespace llvm {
+
+class LoongArchTargetMachine : public LLVMTargetMachine {
+
+public:
+  LoongArchTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
+                         StringRef FS, const TargetOptions &Options,
+                         Optional<Reloc::Model> RM,
+                         Optional<CodeModel::Model> CM, CodeGenOpt::Level OL,
+                         bool JIT);
+  ~LoongArchTargetMachine() override;
+};
+
+} // end namespace llvm
+
+#endif // LLVM_LIB_TARGET_LOONGARCH_LOONGARCHTARGETMACHINE_H
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/CMakeLists.txt b/llvm/lib/Target/LoongArch/MCTargetDesc/CMakeLists.txt
new file mode 100644 (file)
index 0000000..27d5940
--- /dev/null
@@ -0,0 +1,11 @@
+add_llvm_component_library(LLVMLoongArchDesc
+  LoongArchMCTargetDesc.cpp
+
+  LINK_COMPONENTS
+  MC
+  LoongArchInfo
+  Support
+
+  ADD_TO_COMPONENT
+  LoongArch
+  )
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCTargetDesc.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCTargetDesc.cpp
new file mode 100644 (file)
index 0000000..4d9735e
--- /dev/null
@@ -0,0 +1,17 @@
+//===-- LoongArchMCTargetDesc.cpp - LoongArch 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 LoongArch specific target descriptions.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/Compiler.h"
+
+// FIXME: This is just a placeholder to make current commit buildable. Body of
+// this function will be filled in later commits.
+extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchTargetMC() {}
diff --git a/llvm/lib/Target/LoongArch/TargetInfo/CMakeLists.txt b/llvm/lib/Target/LoongArch/TargetInfo/CMakeLists.txt
new file mode 100644 (file)
index 0000000..f53ddba
--- /dev/null
@@ -0,0 +1,9 @@
+add_llvm_component_library(LLVMLoongArchInfo
+  LoongArchTargetInfo.cpp
+
+  LINK_COMPONENTS
+  Support
+
+  ADD_TO_COMPONENT
+  LoongArch
+  )
diff --git a/llvm/lib/Target/LoongArch/TargetInfo/LoongArchTargetInfo.cpp b/llvm/lib/Target/LoongArch/TargetInfo/LoongArchTargetInfo.cpp
new file mode 100644 (file)
index 0000000..64b1a5b
--- /dev/null
@@ -0,0 +1,30 @@
+//===-- LoongArchTargetInfo.cpp - LoongArch 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/LoongArchTargetInfo.h"
+#include "llvm/MC/TargetRegistry.h"
+using namespace llvm;
+
+Target &llvm::getTheLoongArch32Target() {
+  static Target TheLoongArch32Target;
+  return TheLoongArch32Target;
+}
+
+Target &llvm::getTheLoongArch64Target() {
+  static Target TheLoongArch64Target;
+  return TheLoongArch64Target;
+}
+
+extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchTargetInfo() {
+  RegisterTarget<Triple::loongarch32, /*HasJIT=*/false> X(
+      getTheLoongArch32Target(), "loongarch32", "LoongArch (32-bit)",
+      "LoongArch");
+  RegisterTarget<Triple::loongarch64, /*HasJIT=*/false> Y(
+      getTheLoongArch64Target(), "loongarch64", "LoongArch (64-bit)",
+      "LoongArch");
+}
diff --git a/llvm/lib/Target/LoongArch/TargetInfo/LoongArchTargetInfo.h b/llvm/lib/Target/LoongArch/TargetInfo/LoongArchTargetInfo.h
new file mode 100644 (file)
index 0000000..6fc13d5
--- /dev/null
@@ -0,0 +1,21 @@
+//===-- LoongArchTargetInfo.h - LoongArch 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_LOONGARCH_TARGETINFO_LOONGARCHTARGETINFO_H
+#define LLVM_LIB_TARGET_LOONGARCH_TARGETINFO_LOONGARCHTARGETINFO_H
+
+namespace llvm {
+
+class Target;
+
+Target &getTheLoongArch32Target();
+Target &getTheLoongArch64Target();
+
+} // namespace llvm
+
+#endif // LLVM_LIB_TARGET_LOONGARCH_TARGETINFO_LOONGARCHTARGETINFO_H
diff --git a/llvm/test/CodeGen/LoongArch/lit.local.cfg b/llvm/test/CodeGen/LoongArch/lit.local.cfg
new file mode 100644 (file)
index 0000000..2b5a489
--- /dev/null
@@ -0,0 +1,2 @@
+if not 'LoongArch' in config.root.targets:
+    config.unsupported = True
diff --git a/llvm/test/CodeGen/LoongArch/target_support.ll b/llvm/test/CodeGen/LoongArch/target_support.ll
new file mode 100644 (file)
index 0000000..528cdfa
--- /dev/null
@@ -0,0 +1,3 @@
+; RUN: llc --version | FileCheck %s
+; CHECK:    loongarch32 - LoongArch (32-bit)
+; CHECK:    loongarch64 - LoongArch (64-bit)