Skip to content
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

support llvm 17 #187

Merged
merged 10 commits into from
Jan 18, 2024
Merged
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
22 changes: 11 additions & 11 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======================
Expand Down
19 changes: 7 additions & 12 deletions lib/Target/CBackend/CBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <algorithm>
#include <cmath>
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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*/";
Expand Down Expand Up @@ -1424,7 +1423,7 @@ void CWriter::printConstant(Constant *CPV, enum OperandContext Context) {
char Buffer[100];

uint64_t ll = llvm::bit_cast<uint64_t>(V);
sprintf(Buffer, "0x%llx", static_cast<long long>(ll));
snprintf(Buffer, sizeof(Buffer), "0x%llx", static_cast<long long>(ll));

std::string Num(&Buffer[0], &Buffer[6]);
unsigned long Val = strtoul(Num.c_str(), 0, 16);
Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -3267,7 +3265,6 @@ void CWriter::generateHeader(Module &M) {
Out << "(16, &a, &b, &r);\n";
}
Out << "#endif\n";

} else {
Out << " r = ";
if (mask)
Expand Down Expand Up @@ -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!");
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions lib/Target/CBackend/CBackend.h
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
12 changes: 10 additions & 2 deletions test/test_cbe.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

COMMON_CFLAGS = [
'-Iinclude/',
'-g',
'-Wall',
'-Wno-unused-function',
'-Wno-unused-variable',
Expand Down Expand Up @@ -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


Expand Down
5 changes: 2 additions & 3 deletions tools/llvm-cbe/llvm-cbe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/Triple.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/CodeGen/CommandFlags.h"
Expand All @@ -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"
Expand All @@ -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 <llvm/Config/llvm-config.h>
#include <memory>
using namespace llvm;
Expand Down