Skip to content

Commit 5df1bd3

Browse files
committed
[Offload] Introduce offload-tblgen
1 parent b7a93bc commit 5df1bd3

14 files changed

+1444
-2
lines changed

llvm/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ endif()
114114
# LLVM_EXTERNAL_${project}_SOURCE_DIR using LLVM_ALL_PROJECTS
115115
# This allows an easy way of setting up a build directory for llvm and another
116116
# one for llvm+clang+... using the same sources.
117-
set(LLVM_ALL_PROJECTS "bolt;clang;clang-tools-extra;compiler-rt;cross-project-tests;libc;libclc;lld;lldb;mlir;openmp;polly;pstl")
117+
set(LLVM_ALL_PROJECTS "bolt;clang;clang-tools-extra;compiler-rt;cross-project-tests;libc;libclc;lld;lldb;mlir;openmp;polly;pstl;offload")
118118
# The flang project is not yet part of "all" projects (see C++ requirements)
119119
set(LLVM_EXTRA_PROJECTS "flang")
120120
# List of all known projects in the mono repo
@@ -147,7 +147,7 @@ endif()
147147
# As we migrate runtimes to using the bootstrapping build, the set of default runtimes
148148
# should grow as we remove those runtimes from LLVM_ENABLE_PROJECTS above.
149149
set(LLVM_DEFAULT_RUNTIMES "libcxx;libcxxabi;libunwind")
150-
set(LLVM_SUPPORTED_RUNTIMES "libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp;llvm-libgcc")
150+
set(LLVM_SUPPORTED_RUNTIMES "libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp;llvm-libgcc;offload")
151151
set(LLVM_ENABLE_RUNTIMES "" CACHE STRING
152152
"Semicolon-separated list of runtimes to build, or \"all\" (${LLVM_DEFAULT_RUNTIMES}). Supported runtimes are ${LLVM_SUPPORTED_RUNTIMES}.")
153153
if(LLVM_ENABLE_RUNTIMES STREQUAL "all")

llvm/projects/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ endif()
4141

4242
add_llvm_external_project(dragonegg)
4343
add_llvm_external_project(openmp)
44+
add_llvm_external_project(offload)
4445

4546
if(LLVM_INCLUDE_TESTS)
4647
add_llvm_external_project(cross-project-tests)

offload/API/APIDefs.td

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
//===-- APIDefs.td - Base definitions for Offload tablegen -*- tablegen -*-===//
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+
// This file contains the class definitions used to implement the Offload API,
10+
// as well as helper functions used to help populate relevant records.
11+
// See offload/API/README.md for more detailed documentation.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
16+
// Parameter flags
17+
defvar PARAM_IN = 0x1;
18+
defvar PARAM_OUT = 0x2;
19+
defvar PARAM_OPTIONAL = 0x4;
20+
21+
// Prefix for API naming. This could be hard-coded in the future when a value
22+
// is agreed upon.
23+
defvar PREFIX = "OL";
24+
defvar prefix = !tolower(PREFIX);
25+
26+
// Does the type end with '_handle_t'?
27+
class IsHandleType<string Type> {
28+
// size("_handle_t") == 9
29+
bit ret = !if(!lt(!size(Type), 9), 0,
30+
!ne(!find(Type, "_handle_t", !sub(!size(Type), 9)), -1));
31+
}
32+
33+
// Does the type end with '*'?
34+
class IsPointerType<string Type> {
35+
bit ret = !ne(!find(Type, "*", !sub(!size(Type), 1)), -1);
36+
}
37+
38+
class Param<string Type, string Name, string Desc, bits<3> Flags = 0> {
39+
string type = Type;
40+
string name = Name;
41+
string desc = Desc;
42+
bits<3> flags = Flags;
43+
bit IsHandle = IsHandleType<type>.ret;
44+
bit IsPointer = IsPointerType<type>.ret;
45+
}
46+
47+
class Return<string Value, list<string> Conditions = []> {
48+
string value = Value;
49+
list<string> conditions = Conditions;
50+
}
51+
52+
class ShouldCheckHandle<Param P> {
53+
bit ret = !and(P.IsHandle, !eq(!and(PARAM_OPTIONAL, P.flags), 0));
54+
}
55+
56+
class ShouldCheckPointer<Param P> {
57+
bit ret = !and(P.IsPointer, !eq(!and(PARAM_OPTIONAL, P.flags), 0));
58+
}
59+
60+
// For a list of returns that contains a specific return code, find and append
61+
// new conditions to that return
62+
class AppendConditionsToReturn<list<Return> Returns, string ReturnValue,
63+
list<string> Conditions> {
64+
list<Return> ret =
65+
!foreach(Ret, Returns,
66+
!if(!eq(Ret.value, ReturnValue),
67+
Return<Ret.value, Ret.conditions#Conditions>, Ret));
68+
}
69+
70+
// Add null handle checks to a function's return value descriptions
71+
class AddHandleChecksToReturns<list<Param> Params, list<Return> Returns> {
72+
list<string> handle_params =
73+
!foreach(P, Params, !if(ShouldCheckHandle<P>.ret, P.name, ""));
74+
list<string> handle_params_filt =
75+
!filter(param, handle_params, !ne(param, ""));
76+
list<string> handle_param_conds =
77+
!foreach(handle, handle_params_filt, "`NULL == "#handle#"`");
78+
79+
// Does the list of returns already contain ERROR_INVALID_NULL_HANDLE?
80+
bit returns_has_inv_handle = !foldl(
81+
0, Returns, HasErr, Ret,
82+
!or(HasErr, !eq(Ret.value, PREFIX#"_RESULT_ERROR_INVALID_NULL_HANDLE")));
83+
84+
list<Return> returns_out = !if(returns_has_inv_handle,
85+
AppendConditionsToReturn<Returns, PREFIX # "_RESULT_ERROR_INVALID_NULL_HANDLE", handle_param_conds>.ret,
86+
!listconcat(Returns, [Return<PREFIX # "_RESULT_ERROR_INVALID_NULL_HANDLE", handle_param_conds>])
87+
);
88+
}
89+
90+
// Add null pointer checks to a function's return value descriptions
91+
class AddPointerChecksToReturns<list<Param> Params, list<Return> Returns> {
92+
list<string> ptr_params =
93+
!foreach(P, Params, !if(ShouldCheckPointer<P>.ret, P.name, ""));
94+
list<string> ptr_params_filt = !filter(param, ptr_params, !ne(param, ""));
95+
list<string> ptr_param_conds =
96+
!foreach(ptr, ptr_params_filt, "`NULL == "#ptr#"`");
97+
98+
// Does the list of returns already contain ERROR_INVALID_NULL_POINTER?
99+
bit returns_has_inv_ptr = !foldl(
100+
0, Returns, HasErr, Ret,
101+
!or(HasErr, !eq(Ret.value, PREFIX#"_RESULT_ERROR_INVALID_NULL_POINTER")));
102+
list<Return> returns_out = !if(returns_has_inv_ptr,
103+
AppendConditionsToReturn<Returns, PREFIX # "_RESULT_ERROR_INVALID_NULL_POINTER", ptr_param_conds>.ret,
104+
!listconcat(Returns, [Return<PREFIX # "_RESULT_ERROR_INVALID_NULL_POINTER", ptr_param_conds>])
105+
);
106+
}
107+
108+
defvar DefaultReturns = [Return<PREFIX#"_RESULT_SUCCESS">,
109+
Return<PREFIX#"_RESULT_ERROR_UNINITIALIZED">,
110+
Return<PREFIX#"_RESULT_ERROR_DEVICE_LOST">,
111+
Return<PREFIX#"_RESULT_ERROR_ADAPTER_SPECIFIC">];
112+
113+
class APIObject {
114+
string name;
115+
string desc;
116+
}
117+
118+
class Function<string Class> : APIObject {
119+
string api_class = Class;
120+
list<Param> params;
121+
list<Return> returns;
122+
list<string> details = [];
123+
list<string> analogues = [];
124+
125+
list<Return> returns_with_def = !listconcat(DefaultReturns, returns);
126+
list<Return> all_returns = AddPointerChecksToReturns<params,
127+
AddHandleChecksToReturns<params, returns_with_def>.returns_out>.returns_out;
128+
}
129+
130+
class Etor<string Name, string Desc> {
131+
string name = Name;
132+
string desc = Desc;
133+
}
134+
135+
class Enum : APIObject {
136+
// This refers to whether the enumerator descriptions specify a return
137+
// type for functions where this enum may be used as an input type.
138+
// The format is "[$x_some_return_t] Description text"
139+
// (TODO: This is lifted from UR, is it relevant?)
140+
bit is_typed = 0;
141+
142+
list<Etor> etors = [];
143+
}
144+
145+
class StructMember<string Type, string Name, string Desc> {
146+
string type = Type;
147+
string name = Name;
148+
string desc = Desc;
149+
}
150+
151+
defvar DefaultPropStructMembers =
152+
[StructMember<prefix#"_structure_type_t", "stype",
153+
"type of this structure">,
154+
StructMember<"void*", "pNext", "pointer to extension-specific structure">];
155+
156+
class StructHasInheritedMembers<string BaseClass> {
157+
bit ret = !or(!eq(BaseClass, prefix#"_base_properties_t"),
158+
!eq(BaseClass, prefix#"_base_desc_t"));
159+
}
160+
161+
class Struct : APIObject {
162+
string base_class = "";
163+
list<StructMember> members;
164+
list<StructMember> all_members =
165+
!if(StructHasInheritedMembers<base_class>.ret,
166+
DefaultPropStructMembers, [])#members;
167+
}
168+
169+
class Typedef : APIObject { string value; }
170+
171+
class FptrTypedef : APIObject {
172+
list<Param> params;
173+
list<Return> returns;
174+
}
175+
176+
class Macro : APIObject {
177+
string value;
178+
179+
string condition;
180+
string alt_value;
181+
}
182+
183+
class Handle : APIObject;

0 commit comments

Comments
 (0)