From 5a3f239c2842275deb4b8e13c8811c1ca3a29bd7 Mon Sep 17 00:00:00 2001 From: 5c4lar <90229858+5c4lar@users.noreply.github.com> Date: Fri, 19 Jan 2024 03:16:10 +0800 Subject: [PATCH] support llvm 17 (#187) --- .github/workflows/main.yml | 22 +++++++++++----------- CMakeLists.txt | 4 ++-- README.md | 2 +- lib/Target/CBackend/CBackend.cpp | 19 +++++++------------ lib/Target/CBackend/CBackend.h | 2 ++ test/test_cbe.py | 12 ++++++++++-- tools/llvm-cbe/llvm-cbe.cpp | 5 ++--- 7 files changed, 35 insertions(+), 31 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e5ed6a7..8201417 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -32,19 +32,19 @@ jobs: run: | wget https://apt.llvm.org/llvm.sh chmod +x llvm.sh - sudo ./llvm.sh 16 all - sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-16 160 - sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-16 160 - sudo update-alternatives --install /usr/bin/lli lli /usr/bin/lli-16 160 - sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-16 160 + sudo ./llvm.sh 17 all + sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-17 160 + sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-17 160 + sudo update-alternatives --install /usr/bin/lli lli /usr/bin/lli-17 160 + sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-17 160 - name: Setup LLVM and GCC (MacOS) if: ${{ runner.os == 'macOS' }} run: | - brew install llvm@16 - echo "$(brew --prefix llvm@16)/bin" >> $GITHUB_PATH - echo "CC=$(brew --prefix llvm@16)/bin/clang" >> $GITHUB_ENV - echo "CXX=$(brew --prefix llvm@16)/bin/clang++" >> $GITHUB_ENV + brew install llvm@17 + echo "$(brew --prefix llvm@17)/bin" >> $GITHUB_PATH + echo "CC=$(brew --prefix llvm@17)/bin/clang" >> $GITHUB_ENV + echo "CXX=$(brew --prefix llvm@17)/bin/clang++" >> $GITHUB_ENV cd /usr/local/bin ln -s gcc-11 gcc @@ -65,7 +65,7 @@ jobs: - name: Build run: | mkdir build - cmake -S . -B build -DLLVM_INCLUDE_TESTS=On -DLLVM_DIR=/usr/lib/llvm-16/cmake + cmake -S . -B build -DLLVM_INCLUDE_TESTS=On -DLLVM_DIR=/usr/lib/llvm-17/cmake cmake --build build - name: Test @@ -84,7 +84,7 @@ jobs: uses: actions/checkout@master with: repository: llvm/llvm-project - ref: llvmorg-16.0.6 + ref: llvmorg-17.0.6 - name: Checkout uses: actions/checkout@v3 diff --git a/CMakeLists.txt b/CMakeLists.txt index f6e205a..cf6b10e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,8 +15,8 @@ if (NOT DEFINED LLVM_VERSION_MAJOR) set (CMAKE_CXX_STANDARD 17) endif() -if(NOT LLVM_VERSION_MAJOR EQUAL 16) - message(FATAL_ERROR "This project only supports LLVM 16. For older versions of LLVM please check https://github.com/JuliaHubOSS/llvm-cbe/tags") +if(NOT LLVM_VERSION_MAJOR EQUAL 17) + message(FATAL_ERROR "This project only supports LLVM 17. For older versions of LLVM please check https://github.com/JuliaHubOSS/llvm-cbe/tags") endif() add_subdirectory(lib) diff --git a/README.md b/README.md index 5b5e419..e7ae0fa 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This LLVM C backend has been resurrected by Julia Computing with various improve Installation instructions ========================= -This version of the LLVM C backend works with LLVM 16, for older versions please check the [tags](https://github.com/JuliaHubOSS/llvm-cbe/tags). +This version of the LLVM C backend works with LLVM 17, for older versions please check the [tags](https://github.com/JuliaHubOSS/llvm-cbe/tags). Step 1: Installing LLVM ======================= diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index 09ffb0c..d724466 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -23,8 +23,8 @@ #include "llvm/MC/TargetRegistry.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/Host.h" #include "llvm/Support/MathExtras.h" +#include "llvm/TargetParser/Host.h" #include #include @@ -1045,7 +1045,7 @@ static bool isFPCSafeToPrint(const ConstantFP *CFP) { APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &ignored); #if HAVE_PRINTF_A && ENABLE_CBE_PRINTF_A char Buffer[100]; - sprintf(Buffer, "%a", APF.convertToDouble()); + snprintf(Buffer, sizeof(Buffer), "%a", APF.convertToDouble()); if (!strncmp(Buffer, "0x", 2) || !strncmp(Buffer, "-0x", 3) || !strncmp(Buffer, "+0x", 3)) return APF.bitwiseIsEqual(APFloat(atof(Buffer))); @@ -1333,7 +1333,6 @@ void CWriter::printConstant(Constant *CPV, enum OperandContext Context) { printConstant(Zero, ContextCasted); } Out << ")"; - } else { Constant *Zero = Constant::getNullValue(CPV->getType()); Out << "/*UNDEF*/"; @@ -1424,7 +1423,7 @@ void CWriter::printConstant(Constant *CPV, enum OperandContext Context) { char Buffer[100]; uint64_t ll = llvm::bit_cast(V); - sprintf(Buffer, "0x%llx", static_cast(ll)); + snprintf(Buffer, sizeof(Buffer), "0x%llx", static_cast(ll)); std::string Num(&Buffer[0], &Buffer[6]); unsigned long Val = strtoul(Num.c_str(), 0, 16); @@ -1450,7 +1449,7 @@ void CWriter::printConstant(Constant *CPV, enum OperandContext Context) { #if HAVE_PRINTF_A && ENABLE_CBE_PRINTF_A // Print out the constant as a floating point number. char Buffer[100]; - sprintf(Buffer, "%a", V); + snprintf(Buffer, sizeof(Buffer), "%a", V); Num = Buffer; #else Num = ftostr(FPC->getValueAPF()); @@ -1702,7 +1701,7 @@ std::string CWriter::GetValueName(const Value *Operand) { if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_')) { char buffer[5]; - sprintf(buffer, "_%x_", ch); + snprintf(buffer, sizeof(buffer), "_%x_", ch); VarName += buffer; } else VarName += ch; @@ -3125,7 +3124,6 @@ void CWriter::generateHeader(Module &M) { Out << ") & " << mask; Out << ";\n"; } - } else if (OpTy->getPrimitiveSizeInBits() > 64) { Out << " r;\n"; Out << "#ifndef __emulate_i128\n"; @@ -3267,7 +3265,6 @@ void CWriter::generateHeader(Module &M) { Out << "(16, &a, &b, &r);\n"; } Out << "#endif\n"; - } else { Out << " r = "; if (mask) @@ -3569,7 +3566,6 @@ void CWriter::printFloatingPointConstants(const Constant *C) { Out << "static const ConstantFP128Ty FPConstant" << Counter << " = { 0x" << utohexstr(p[0]) << ", 0x" << utohexstr(p[1]) << "}; /* Long double constant */\n"; - } else { errorWithMessage("Unknown float type!"); } @@ -3956,7 +3952,6 @@ void CWriter::visitSwitchInst(SwitchInst &SI) { printPHICopiesForSuccessor(SI.getParent(), SI.getDefaultDest(), 2); printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2); Out << "\n"; - } else if (NumBits <= 64) { // model as a switch statement Out << " switch ("; writeOperand(Cond); @@ -3979,7 +3974,6 @@ void CWriter::visitSwitchInst(SwitchInst &SI) { Out << " break;\n"; } Out << " }\n"; - } else { // model as a series of if statements Out << " "; for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); i != e; @@ -4707,7 +4701,6 @@ void CWriter::printIntrinsicDefinition(FunctionType *funT, unsigned Opcode, Out << " r = 0 /* llvm.is.constant */;\n"; break; } - } else { // handle FP ops const char *suffix; @@ -4825,6 +4818,7 @@ bool CWriter::lowerIntrinsics(Function &F) { case Intrinsic::stackprotector: case Intrinsic::dbg_value: case Intrinsic::dbg_declare: + case Intrinsic::dbg_assign: case Intrinsic::umax: case Intrinsic::umin: case Intrinsic::smin: @@ -4990,6 +4984,7 @@ bool CWriter::visitBuiltinCall(CallInst &I, Intrinsic::ID ID) { } case Intrinsic::dbg_value: case Intrinsic::dbg_declare: + case Intrinsic::dbg_assign: return true; // ignore these intrinsics case Intrinsic::vastart: headerUseStdarg(); diff --git a/lib/Target/CBackend/CBackend.h b/lib/Target/CBackend/CBackend.h index 64b0a07..85cd33b 100644 --- a/lib/Target/CBackend/CBackend.h +++ b/lib/Target/CBackend/CBackend.h @@ -1,4 +1,6 @@ #include "CTargetMachine.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/DenseMapInfoVariant.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/SmallString.h" diff --git a/test/test_cbe.py b/test/test_cbe.py index ba712ef..47c53b1 100644 --- a/test/test_cbe.py +++ b/test/test_cbe.py @@ -27,7 +27,6 @@ COMMON_CFLAGS = [ '-Iinclude/', - '-g', '-Wall', '-Wno-unused-function', '-Wno-unused-variable', @@ -107,7 +106,16 @@ def check_no_output(args, cwd): def compile_gcc(c_filename, output_filename, flags=None): flags = flags or [] - check_no_output([GCC, c_filename, '-o', output_filename] + GCCFLAGS + flags, os.path.dirname(output_filename)) + try: + check_no_output([GCC, c_filename, '-o', output_filename] + GCCFLAGS + flags, os.path.dirname(output_filename)) + except Exception as e: + msg_stream = io.StringIO() + with open(c_filename, 'r') as f: + print(f"source code:", file=msg_stream) + print(f.read(), file=msg_stream) + print(file=msg_stream) + raise Exception(msg_stream.getvalue()) from e + return output_filename diff --git a/tools/llvm-cbe/llvm-cbe.cpp b/tools/llvm-cbe/llvm-cbe.cpp index 9aef0c6..8e4e930 100644 --- a/tools/llvm-cbe/llvm-cbe.cpp +++ b/tools/llvm-cbe/llvm-cbe.cpp @@ -13,7 +13,6 @@ // //===----------------------------------------------------------------------===// -#include "llvm/ADT/Triple.h" #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/CodeGen/CommandFlags.h" @@ -28,14 +27,12 @@ #include "llvm/IRReader/IRReader.h" #include "llvm/InitializePasses.h" #include "llvm/MC/MCTargetOptionsCommandFlags.h" -#include "llvm/MC/SubtargetFeature.h" #include "llvm/MC/TargetRegistry.h" #include "llvm/Pass.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/FormattedStream.h" -#include "llvm/Support/Host.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/PluginLoader.h" #include "llvm/Support/PrettyStackTrace.h" @@ -44,6 +41,8 @@ #include "llvm/Support/TargetSelect.h" #include "llvm/Support/ToolOutputFile.h" #include "llvm/Target/TargetMachine.h" +#include "llvm/TargetParser/Host.h" +#include "llvm/TargetParser/SubtargetFeature.h" #include #include using namespace llvm;