Skip to content

Commit

Permalink
gollvm: encode platform info in CallingConvId
Browse files Browse the repository at this point in the history
Previously gollvm encodes platform info in llvm::CallingConv::Id which
may not be unique among all platforms. Replacing it with a local enum
solves the problem.

Change-Id: Ief01878a8c0b3a1200b575087196a758314c5c22
Reviewed-on: https://go-review.googlesource.com/c/gollvm/+/427737
Reviewed-by: Ian Lance Taylor <[email protected]>
Reviewed-by: Than McIntosh <[email protected]>
  • Loading branch information
melonedo authored and thanm committed Jan 17, 2023
1 parent 3452ec6 commit f059550
Show file tree
Hide file tree
Showing 28 changed files with 128 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ TEST(BackendCoreTests, ComplexTypes) {
Type *ft = Type::getFloatTy(C);
Type *dt = Type::getDoubleTy(C);
std::unique_ptr<Backend> be(go_get_backend(C, llvm::CallingConv::X86_64_SysV));
std::unique_ptr<Backend> be(go_get_backend(C, gollvm::driver::CallingConvId::X86_64_SysV));
Btype *c32 = be->complex_type(64);
ASSERT_TRUE(c32 != NULL);
ASSERT_EQ(c32->type(), mkTwoFieldLLvmStruct(C, ft, ft));
Expand Down
2 changes: 1 addition & 1 deletion bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ add_dependencies(LLVMCppGoFrontEnd libmpfr libmpc libgmp)

include_directories(${EXTINSTALLDIR}/include)
include_directories(${GOFRONTEND_SOURCE_DIR})

include_directories(${DRIVER_UTILS_SOURCE_DIR})
2 changes: 1 addition & 1 deletion bridge/go-llvm-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@

class Backend;

extern Backend *go_get_backend(llvm::LLVMContext &Context, llvm::CallingConv::ID cconv);
extern Backend *go_get_backend(llvm::LLVMContext &Context, gollvm::driver::CallingConvId cconv);

#endif // !defined(GO_LLVM_BACKEND_H)
33 changes: 18 additions & 15 deletions bridge/go-llvm-cabi-oracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "llvm/IR/DataLayout.h"
#include "llvm/Support/raw_ostream.h"

#include "CallingConv.h"

//......................................................................

// Given an LLVM type, classify it according to whether it would
Expand Down Expand Up @@ -146,20 +148,20 @@ EightByteInfo::EightByteInfo(Btype *bt, TypeManager *tmgr)
: typeManager_(tmgr)
{
explode(bt);
llvm::CallingConv::ID cconv = tmgr->callingConv();
gollvm::driver::CallingConvId cconv = tmgr->callingConv();
switch (cconv) {
case llvm::CallingConv::X86_64_SysV:
case gollvm::driver::CallingConvId::X86_64_SysV:
determineABITypesForX86_64_SysV();
break;
case llvm::CallingConv::ARM_AAPCS:
case gollvm::driver::CallingConvId::ARM_AAPCS:
setHFA();
if (getHFA().number == 0 && tmgr->typeSize(bt) <= 16) {
// For HFA and indirect cases, we don't need do this.
determineABITypesForARM_AAPCS();
}
break;
default:
llvm::errs() << "unsupported llvm::CallingConv::ID " << cconv << "\n";
llvm::errs() << "unsupported gollvm::driver::CallingConvId " << static_cast<int>(cconv) << "\n";
break;
}
}
Expand Down Expand Up @@ -544,18 +546,18 @@ class ABIState {
public:
ABIState(TypeManager *typm) : argCount_(0) {
assert(typm != nullptr);
llvm::CallingConv::ID cconv = typm->callingConv();
gollvm::driver::CallingConvId cconv = typm->callingConv();
switch (cconv) {
case llvm::CallingConv::X86_64_SysV:
case gollvm::driver::CallingConvId::X86_64_SysV:
availIntRegs_ = 6;
availSSERegs_ = 8;
break;
case llvm::CallingConv::ARM_AAPCS:
case gollvm::driver::CallingConvId::ARM_AAPCS:
availIntRegs_ = 8;
availSIMDFPRegs_ = 8;
break;
default:
llvm::errs() << "unsupported llvm::CallingConv::ID " << cconv << "\n";
llvm::errs() << "unsupported gollvm::driver::CallingConvId " << static_cast<int>(cconv) << "\n";
break;
}
}
Expand Down Expand Up @@ -610,7 +612,7 @@ CABIOracle::CABIOracle(const std::vector<Btype *> &fcnParamTypes,
, fcnTypeForABI_(nullptr)
, typeManager_(typeManager)
, followsCabi_(followsCabi)
, ccID_(llvm::CallingConv::MaxID)
, ccID_(gollvm::driver::CallingConvId::MaxID)
, cc_(nullptr)
{
setCC();
Expand All @@ -624,7 +626,7 @@ CABIOracle::CABIOracle(BFunctionType *ft,
, fcnTypeForABI_(nullptr)
, typeManager_(typeManager)
, followsCabi_(ft->followsCabi())
, ccID_(llvm::CallingConv::MaxID)
, ccID_(gollvm::driver::CallingConvId::MaxID)
, cc_(nullptr)
{
setCC();
Expand All @@ -636,21 +638,22 @@ void CABIOracle::setCC()
assert(typeManager_ != nullptr);
ccID_ = typeManager_->callingConv();
// Supported architectures at present.
assert(ccID_ == llvm::CallingConv::X86_64_SysV ||
ccID_ == llvm::CallingConv::ARM_AAPCS);
assert(ccID_ == gollvm::driver::CallingConvId::X86_64_SysV ||
ccID_ == gollvm::driver::CallingConvId::ARM_AAPCS ||
ccID_ == gollvm::driver::CallingConvId::RISCV64_C);

if (cc_ != nullptr) {
return;
}
switch (ccID_) {
case llvm::CallingConv::X86_64_SysV:
case gollvm::driver::CallingConvId::X86_64_SysV:
cc_ = std::unique_ptr<CABIOracleArgumentAnalyzer>(new CABIOracleX86_64_SysV(typeManager_));
break;
case llvm::CallingConv::ARM_AAPCS:
case gollvm::driver::CallingConvId::ARM_AAPCS:
cc_ = std::unique_ptr<CABIOracleArgumentAnalyzer>(new CABIOracleARM_AAPCS(typeManager_));
break;
default:
llvm::errs() << "unsupported llvm::CallingConv::ID " << ccID_ << "\n";
llvm::errs() << "unsupported gollvm::driver::CallingConvId " << static_cast<int>(ccID_) << "\n";
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions bridge/go-llvm-cabi-oracle.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#ifndef LLVMGOFRONTEND_GO_LLVM_CABI_ORACLE_H
#define LLVMGOFRONTEND_GO_LLVM_CABI_ORACLE_H

#include "CallingConv.h"
#include "go-llvm-btype.h"
#include "llvm/IR/CallingConv.h"

class TypeManager;
class EightByteInfo;
Expand Down Expand Up @@ -198,7 +198,7 @@ class CABIOracle {
TypeManager *typeManager_;
std::vector<CABIParamInfo> infov_;
bool followsCabi_;
llvm::CallingConv::ID ccID_;
gollvm::driver::CallingConvId ccID_;
std::unique_ptr<CABIOracleArgumentAnalyzer> cc_;

// The main entry for cabi analysis.
Expand Down
2 changes: 1 addition & 1 deletion bridge/go-llvm-typemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "llvm/IR/Type.h"

TypeManager::TypeManager(llvm::LLVMContext &context,
llvm::CallingConv::ID conv,
gollvm::driver::CallingConvId conv,
unsigned addrspace)
: context_(context)
, datalayout_(nullptr)
Expand Down
8 changes: 4 additions & 4 deletions bridge/go-llvm-typemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "namegen.h"
#include "backend.h"

#include "llvm/IR/CallingConv.h"
#include "CallingConv.h"

namespace llvm {
class DataLayout;
Expand All @@ -43,7 +43,7 @@ enum PTDisp { Concrete, Placeholder };
class TypeManager {
public:
TypeManager(llvm::LLVMContext &context,
llvm::CallingConv::ID cconv,
gollvm::driver::CallingConvId cconv,
unsigned addrspace);
~TypeManager();

Expand Down Expand Up @@ -246,7 +246,7 @@ class TypeManager {
const llvm::DataLayout *datalayout() const { return datalayout_; }

// Calling convention
llvm::CallingConv::ID callingConv() const { return cconv_; }
gollvm::driver::CallingConvId callingConv() const { return cconv_; }

// For named types, this returns the declared type name. If a type
// is unnamed, then it returns a stringified representation of the
Expand Down Expand Up @@ -287,7 +287,7 @@ class TypeManager {
// Context information needed for the LLVM backend.
llvm::LLVMContext &context_;
const llvm::DataLayout *datalayout_;
llvm::CallingConv::ID cconv_;
gollvm::driver::CallingConvId cconv_;
unsigned addressSpace_;
unsigned traceLevel_;

Expand Down
13 changes: 9 additions & 4 deletions bridge/go-llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Llvm_backend::Llvm_backend(llvm::LLVMContext &context,
Llvm_linemap *linemap,
unsigned addrspace,
llvm::Triple triple,
llvm::CallingConv::ID cconv)
gollvm::driver::CallingConvId cconv)
: TypeManager(context, cconv, addrspace)
, context_(context)
, module_(module)
Expand Down Expand Up @@ -83,16 +83,21 @@ Llvm_backend::Llvm_backend(llvm::LLVMContext &context,
if (!module_) {
ownModule_.reset(new llvm::Module("gomodule", context));
switch (cconv) {
case llvm::CallingConv::X86_64_SysV:
case gollvm::driver::CallingConvId::X86_64_SysV:
ownModule_->setTargetTriple("x86_64-unknown-linux-gnu");
ownModule_->setDataLayout("e-m:e-i64:64-f80:128-n8:16:32:64-S128");
triple_ = llvm::Triple("x86_64-unknown-linux-gnu");
break;
case llvm::CallingConv::ARM_AAPCS:
case gollvm::driver::CallingConvId::ARM_AAPCS:
ownModule_->setTargetTriple("aarch64-unknown-linux-gnu");
ownModule_->setDataLayout("e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128");
triple_ = llvm::Triple("aarch64-unknown-linux-gnu");
break;
case gollvm::driver::CallingConvId::RISCV64_C:
ownModule_->setTargetTriple("riscv64-unknown-linux-gnu");
ownModule_->setDataLayout("e-m:e-p:64:64-i64:64-i128:128-n64-S128");
triple_ = llvm::Triple("riscv64-unknown-linux-gnu");
break;
default:
std::cerr <<"Unsupported calling convention\n";
}
Expand Down Expand Up @@ -4158,6 +4163,6 @@ const char *go_localize_identifier(const char *ident) { return ident; }

// Return a new backend generator.

Backend *go_get_backend(llvm::LLVMContext &context, llvm::CallingConv::ID cconv) {
Backend *go_get_backend(llvm::LLVMContext &context, gollvm::driver::CallingConvId cconv) {
return new Llvm_backend(context, nullptr, nullptr, 0, llvm::Triple(), cconv);
}
2 changes: 1 addition & 1 deletion bridge/go-llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Llvm_backend : public Backend, public TypeManager, public NameGen {
unsigned addrspace,
llvm::Triple triple,
/* Temporarily set the parameter as optional to workaround the unit tests. */
llvm::CallingConv::ID cconv=llvm::CallingConv::X86_64_SysV);
gollvm::driver::CallingConvId cconv=gollvm::driver::CallingConvId::X86_64_SysV);
~Llvm_backend();

// Types.
Expand Down
25 changes: 25 additions & 0 deletions driver/CallingConv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- CallingConv.h -----------------------------------------------------===//
//
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
//===----------------------------------------------------------------------===//
//
// Defines the CallingConvId type.
//
//===----------------------------------------------------------------------===//

#ifndef GOLLVM_DRIVER_CALLINGCONV_H
#define GOLLVM_DRIVER_CALLINGCONV_H

namespace gollvm {
namespace driver {

// Used inplace of llvm::CallingConv::Id which does not record arch and cpu
enum class CallingConvId { X86_64_SysV, ARM_AAPCS, RISCV64_C, MaxID };

} // end namespace driver
} // end namespace gollvm

#endif // GOLLVM_DRIVER_CALLINGCONV_H
12 changes: 8 additions & 4 deletions driver/CompileGo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Action.h"
#include "ArchCpuSetup.h"
#include "Artifact.h"
#include "CallingConv.h"
#include "Driver.h"
#include "ToolChain.h"

Expand Down Expand Up @@ -105,7 +106,7 @@ class CompileGoImpl {
Triple triple_;
const ToolChain &toolchain_;
Driver &driver_;
CallingConv::ID cconv_;
CallingConvId cconv_;
LLVMContext context_;
const char *progname_;
std::string executablePath_;
Expand Down Expand Up @@ -154,7 +155,7 @@ CompileGoImpl::CompileGoImpl(CompileGo &cg,
triple_(tc.driver().triple()),
toolchain_(tc),
driver_(tc.driver()),
cconv_(CallingConv::MaxID),
cconv_(CallingConvId::MaxID),
progname_(tc.driver().progname()),
executablePath_(executablePath),
args_(tc.driver().args()),
Expand Down Expand Up @@ -723,10 +724,13 @@ void CompileGoImpl::setCConv()
assert(triple_.getArch() != Triple::UnknownArch);
switch (triple_.getArch()) {
case Triple::x86_64:
cconv_ = CallingConv::X86_64_SysV;
cconv_ = CallingConvId::X86_64_SysV;
break;
case Triple::aarch64:
cconv_ = CallingConv::ARM_AAPCS;
cconv_ = CallingConvId::ARM_AAPCS;
break;
case Triple::riscv64:
cconv_ = CallingConvId::RISCV64_C;
break;
default:
errs() << "currently Gollvm is not supported on architecture "
Expand Down
2 changes: 2 additions & 0 deletions passes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ add_llvm_library(LLVMCppGoPasses

DEPENDS intrinsics_gen
)

include_directories(${DRIVER_UTILS_SOURCE_DIR})
2 changes: 1 addition & 1 deletion passes/GoStatepoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//
//===----------------------------------------------------------------------===//

#include "CallingConv.h"
#include "GoStatepoints.h"
#include "GoStackMap.h"
#include "GollvmPasses.h"
Expand All @@ -35,7 +36,6 @@
#include "llvm/IR/Argument.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
Expand Down
4 changes: 2 additions & 2 deletions unittests/BackendCore/BackendArrayStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ using namespace goBackendUnitTests;
namespace {

class BackendArrayStructTests
: public testing::TestWithParam<llvm::CallingConv::ID> {};
: public testing::TestWithParam<gollvm::driver::CallingConvId> {};

INSTANTIATE_TEST_SUITE_P(
UnitTest, BackendArrayStructTests,
goBackendUnitTests::CConvs,
goBackendUnitTests::cconvs(),
[](const testing::TestParamInfo<BackendArrayStructTests::ParamType> &info) {
std::string name = goBackendUnitTests::ccName(info.param);
return name;
Expand Down
Loading

0 comments on commit f059550

Please sign in to comment.