Skip to content

Commit

Permalink
[SOL][BPF] Enable Solana BPF extensions as subtarget feature (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmakarov authored Apr 12, 2021
1 parent 1484545 commit 91de63f
Show file tree
Hide file tree
Showing 44 changed files with 302 additions and 135 deletions.
24 changes: 20 additions & 4 deletions clang/lib/Basic/Targets/BPF.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LLVM_LIBRARY_VISIBILITY BPFTargetInfo : public TargetInfo {
static const Builtin::Info BuiltinInfo[];

public:
BPFTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
BPFTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
: TargetInfo(Triple) {
LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
SizeType = UnsignedLong;
Expand All @@ -34,10 +34,25 @@ class LLVM_LIBRARY_VISIBILITY BPFTargetInfo : public TargetInfo {
IntMaxType = SignedLong;
Int64Type = SignedLong;
RegParmMax = 5;
auto isSolana = false;
for (auto& it : Opts.FeaturesAsWritten) {
if (it == "+solana") {
isSolana = true;
break;
}
}
if (Triple.getArch() == llvm::Triple::bpfeb) {
resetDataLayout("E-m:e-p:64:64-i64:64-n32:64-S128");
if (isSolana) {
resetDataLayout("E-m:e-p:64:64-i64:64-n32:64-S128");
} else {
resetDataLayout("E-m:e-p:64:64-i64:64-i128:128-n32:64-S128");
}
} else {
resetDataLayout("e-m:e-p:64:64-i64:64-n32:64-S128");
if (isSolana) {
resetDataLayout("e-m:e-p:64:64-i64:64-n32:64-S128");
} else {
resetDataLayout("e-m:e-p:64:64-i64:64-i128:128-n32:64-S128");
}
}
MaxAtomicPromoteWidth = 64;
MaxAtomicInlineWidth = 64;
Expand All @@ -48,7 +63,8 @@ class LLVM_LIBRARY_VISIBILITY BPFTargetInfo : public TargetInfo {
MacroBuilder &Builder) const override;

bool hasFeature(StringRef Feature) const override {
return Feature == "bpf" || Feature == "alu32" || Feature == "dwarfris";
return Feature == "bpf" || Feature == "alu32" || Feature == "dwarfris" ||
Feature == "solana";
}

void setFeatureEnabled(llvm::StringMap<bool> &Features, StringRef Name,
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_clang_library(clangDriver
ToolChain.cpp
ToolChains/Arch/AArch64.cpp
ToolChains/Arch/ARM.cpp
ToolChains/Arch/BPF.cpp
ToolChains/Arch/Mips.cpp
ToolChains/Arch/PPC.cpp
ToolChains/Arch/RISCV.cpp
Expand Down
56 changes: 56 additions & 0 deletions clang/lib/Driver/ToolChains/Arch/BPF.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//===--- BPF.cpp - Tools Implementations ------------------------*- 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
//
//===----------------------------------------------------------------------===//

#include "BPF.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Driver/Options.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Option/ArgList.h"

using namespace clang::driver;
using namespace clang::driver::tools;
using namespace clang;
using namespace llvm::opt;

// Decode AArch64 features from string like +[no]featureA+[no]featureB+...
static bool DecodeBPFFeatures(const Driver &D, StringRef text,
std::vector<StringRef> &Features) {
SmallVector<StringRef, 8> Split;
text.split(Split, StringRef("+"), -1, false);

for (StringRef Feature : Split) {
if (Feature == "solana")
Features.push_back("+solana");
else
return false;
}
return true;
}

static bool
getBPFArchFeaturesFromMarch(const Driver &D, StringRef March,
const ArgList &Args,
std::vector<StringRef> &Features) {
std::string MarchLowerCase = March.lower();
std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");

return (Split.first == "bpfel" || Split.first == "bpfeb") &&
(Split.second.size() == 0 || DecodeBPFFeatures(D, Split.second, Features));
}

void bpf::getBPFTargetFeatures(const Driver &D, const ArgList &Args,
std::vector<StringRef> &Features) {
printf("CHECK BPF FEATURES\n");
Arg *A;
bool success = true;
if ((A = Args.getLastArg(options::OPT_march_EQ)))
success = getBPFArchFeaturesFromMarch(D, A->getValue(), Args, Features);
if (!success)
D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
}
31 changes: 31 additions & 0 deletions clang/lib/Driver/ToolChains/Arch/BPF.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===--- BPF.h - BPF-specific Tool Helpers ----------------------*- 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_CLANG_LIB_DRIVER_TOOLCHAINS_ARCH_BPF_H
#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ARCH_BPF_H

#include "clang/Driver/Driver.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Option/Option.h"
#include <string>
#include <vector>

namespace clang {
namespace driver {
namespace tools {
namespace bpf {

void getBPFTargetFeatures(const Driver &D, const llvm::opt::ArgList &Args,
std::vector<llvm::StringRef> &Features);

} // end namespace bpf
} // namespace tools
} // end namespace driver
} // end namespace clang

#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ARCH_BPF_H
5 changes: 5 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "AMDGPU.h"
#include "Arch/AArch64.h"
#include "Arch/ARM.h"
#include "Arch/BPF.h"
#include "Arch/Mips.h"
#include "Arch/PPC.h"
#include "Arch/RISCV.h"
Expand Down Expand Up @@ -347,6 +348,10 @@ static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
case llvm::Triple::aarch64_be:
aarch64::getAArch64TargetFeatures(D, Triple, Args, Features);
break;
case llvm::Triple::bpfeb:
case llvm::Triple::bpfel:
bpf::getBPFTargetFeatures(D, Args, Features);
break;
case llvm::Triple::x86:
case llvm::Triple::x86_64:
x86::getX86TargetFeatures(D, Triple, Args, Features);
Expand Down
12 changes: 10 additions & 2 deletions clang/test/CodeGen/target-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,19 @@

// RUN: %clang_cc1 -triple bpfel -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=BPFEL
// BPFEL: target datalayout = "e-m:e-p:64:64-i64:64-n32:64-S128"
// BPFEL: target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"

// RUN: %clang_cc1 -triple bpfeb -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=BPFEB
// BPFEB: target datalayout = "E-m:e-p:64:64-i64:64-n32:64-S128"
// BPFEB: target datalayout = "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128"

// RUN: %clang_cc1 -triple bpfel -target-feature +solana -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=BPFELSOL
// BPFELSOL: target datalayout = "e-m:e-p:64:64-i64:64-n32:64-S128"

// RUN: %clang_cc1 -triple bpfeb -target-feature +solana -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=BPFEBSOL
// BPFEBSOL: target datalayout = "E-m:e-p:64:64-i64:64-n32:64-S128"

// RUN: %clang_cc1 -triple ve -o - -emit-llvm %s | \
// RUN: FileCheck %s -check-prefix=VE
Expand Down
3 changes: 0 additions & 3 deletions llvm/lib/IR/DiagnosticInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,6 @@ void DiagnosticInfoUnsupported::print(DiagnosticPrinter &DP) const {
std::string Str;
raw_string_ostream OS(Str);

dbgs() << "Warning" << getLocationStr() << ": in function " << getFunction().getName() << ' '
<< *getFunction().getFunctionType() << ": " << Msg << '\n';

OS << getLocationStr() << ": in function " << getFunction().getName() << ' '
<< *getFunction().getFunctionType() << ": " << Msg << '\n';
OS.flush();
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Target/BPF/BPF.td
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def ALU32 : SubtargetFeature<"alu32", "HasAlu32", "true",
def DwarfRIS: SubtargetFeature<"dwarfris", "UseDwarfRIS", "true",
"Disable MCAsmInfo DwarfUsesRelocationsAcrossSections">;

def FeatureSolana : SubtargetFeature<"solana", "IsSolana", "true",
"Enable Solana extensions">;

def BPFInstPrinter : AsmWriter {
string AsmWriterClassName = "InstPrinter";
bit isMCAsmWriter = 1;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/BPF/BPFCallingConv.td
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def CC_BPF64 : CallingConv<[
CCIfType<[ i8, i16, i32 ], CCPromoteToType<i64>>,

// All arguments get passed in integer registers if there is space.
CCIfType<[i64], CCAssignToReg<[R1, R2, R3, R4, R5]>>,
CCIfType<[i64], CCAssignToReg<[ R1, R2, R3, R4, R5 ]>>,

// Could be assigned to the stack in 8-byte aligned units, but unsupported
CCAssignToStack<8, 8>
Expand Down
Loading

0 comments on commit 91de63f

Please sign in to comment.