Skip to content

Commit f86bb0d

Browse files
committed
[LLVMABI] Added Support for the BPF ABI
1 parent c180e24 commit f86bb0d

File tree

7 files changed

+562
-0
lines changed

7 files changed

+562
-0
lines changed

llvm/examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
add_subdirectory(ABI)
12
add_subdirectory(BrainF)
23
add_subdirectory(Fibonacci)
34
add_subdirectory(HowToUseJIT)

llvm/include/llvm/ABI/ABIInfo.h

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
//===- ABIInfo.h - ABI Information -----------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file defines the interfaces used by frontends to get ABI information.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_ABI_ABIINFO_H
15+
#define LLVM_ABI_ABIINFO_H
16+
17+
#include "llvm/ABI/ABIType.h"
18+
#include "llvm/ADT/ArrayRef.h"
19+
#include "llvm/IR/Type.h"
20+
#include "llvm/Support/ErrorHandling.h"
21+
#include <memory>
22+
#include <vector>
23+
24+
// TODO: cleanup namespaces
25+
namespace llvm {
26+
class LLVMContext;
27+
28+
namespace abi {
29+
30+
/// Argument or return value ABI classification
31+
enum class ABIArgKind {
32+
Direct, // Passed directly in registers or on the stack
33+
Indirect, // Passed indirectly via a hidden pointer
34+
Ignore, // Ignored like empty structs
35+
Expand, // Expand into multiple arguments
36+
Coerce // Coerce to different type
37+
};
38+
39+
/// Information about how to handle an argument or return value
40+
class ABIArgInfo {
41+
ABIArgKind Kind;
42+
const ABIType *CoerceToABIType; // Target ABI type for coercion
43+
Type *CoerceToLLVMType; // Corresponding LLVM type
44+
unsigned DirectOffset; // Offset for direct passing
45+
bool CanBeFlattened; // Whether the type can be flattened
46+
bool InReg; // Whether to pass in registers
47+
48+
/// For Expand kind - holds component types
49+
std::vector<ABIArgInfo> ExpandedArgs;
50+
51+
public:
52+
ABIArgInfo(ABIArgKind Kind, const ABIType *CoerceToABIType = nullptr,
53+
Type *CoerceToLLVMType = nullptr, unsigned DirectOffset = 0,
54+
bool CanBeFlattened = false, bool InReg = false)
55+
: Kind(Kind), CoerceToABIType(CoerceToABIType),
56+
CoerceToLLVMType(CoerceToLLVMType), DirectOffset(DirectOffset),
57+
CanBeFlattened(CanBeFlattened), InReg(InReg) {}
58+
59+
// Factory methods
60+
static ABIArgInfo getDirect(const ABIType *ABITy = nullptr,
61+
Type *LLVMTy = nullptr,
62+
unsigned Offset = 0,
63+
bool CanBeFlattened = false,
64+
bool InReg = false) {
65+
return ABIArgInfo(ABIArgKind::Direct, ABITy, LLVMTy,
66+
Offset, CanBeFlattened, InReg);
67+
}
68+
69+
static ABIArgInfo getIndirect(const ABIType *ABITy = nullptr,
70+
bool InReg = false) {
71+
return ABIArgInfo(ABIArgKind::Indirect, ABITy, nullptr,
72+
0, false, InReg);
73+
}
74+
75+
static ABIArgInfo getIgnore() {
76+
return ABIArgInfo(ABIArgKind::Ignore);
77+
}
78+
79+
static ABIArgInfo getCoerce(const ABIType *ABITy,
80+
Type *LLVMTy = nullptr) {
81+
return ABIArgInfo(ABIArgKind::Coerce, ABITy, LLVMTy);
82+
}
83+
84+
static ABIArgInfo getExpand() {
85+
return ABIArgInfo(ABIArgKind::Expand);
86+
}
87+
88+
// Accessors
89+
ABIArgKind getKind() const { return Kind; }
90+
const ABIType *getCoerceToABIType() const { return CoerceToABIType; }
91+
Type *getCoerceToLLVMType() const { return CoerceToLLVMType; }
92+
unsigned getDirectOffset() const { return DirectOffset; }
93+
bool canBeFlattened() const { return CanBeFlattened; }
94+
bool getInReg() const { return InReg; }
95+
96+
void setCoerceToLLVMType(Type *T) { CoerceToLLVMType = T; }
97+
98+
// Predicates
99+
bool isDirect() const { return Kind == ABIArgKind::Direct; }
100+
bool isIndirect() const { return Kind == ABIArgKind::Indirect; }
101+
bool isIgnore() const { return Kind == ABIArgKind::Ignore; }
102+
bool isCoerce() const { return Kind == ABIArgKind::Coerce; }
103+
bool isExpand() const { return Kind == ABIArgKind::Expand; }
104+
105+
// Expand kind management
106+
void setExpandedArgs(ArrayRef<ABIArgInfo> Args) {
107+
assert(Kind == ABIArgKind::Expand && "Not an expand kind");
108+
ExpandedArgs.assign(Args.begin(), Args.end());
109+
}
110+
111+
ArrayRef<ABIArgInfo> getExpandedArgs() const {
112+
assert(Kind == ABIArgKind::Expand && "Not an expand kind");
113+
return ExpandedArgs;
114+
}
115+
};
116+
117+
/// Base class for target-specific ABI information
118+
class ABIInfo {
119+
public:
120+
virtual ~ABIInfo() = default;
121+
122+
/// Classify how a type should be passed as an argument
123+
virtual ABIArgInfo classifyArgumentType(const ABIType *Ty) = 0;
124+
125+
/// Classify how a type should be returned
126+
virtual ABIArgInfo classifyReturnType(const ABIType *Ty) = 0;
127+
128+
/// Convert an ABIType to the corresponding LLVM IR type
129+
virtual Type *getLLVMType(const ABIType *Ty, LLVMContext &Context) = 0;
130+
};
131+
132+
/// Create target-specific ABI information based on the target triple
133+
std::unique_ptr<ABIInfo> createABIInfo(StringRef TargetTriple);
134+
135+
} // namespace abi
136+
} // namespace llvm
137+
138+
#endif // LLVM_ABI_ABIINFO_H

0 commit comments

Comments
 (0)