Skip to content

Commit 0918e80

Browse files
committed
extern var as kernel inputs
1 parent 2a15ec6 commit 0918e80

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/clangcxx/src/llvm/ASTConsumer.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@ auto FunctionBuilderBuilder::build(const clang::FunctionDecl *S, bool allowKerne
13751375
}
13761376

13771377
// collect args
1378-
for (auto param : params) {
1378+
auto collect_arg = [&] (const VarDecl* param) -> const luisa::compute::RefExpr* {
13791379
auto Ty = param->getType();
13801380
if (auto lcType = db->FindOrAddType(Ty, param->getBeginLoc())) {
13811381
const luisa::compute::RefExpr *local = nullptr;
@@ -1408,9 +1408,20 @@ auto FunctionBuilderBuilder::build(const clang::FunctionDecl *S, bool allowKerne
14081408
} break;
14091409
}
14101410
stack.SetLocal(param, local);
1411+
return local;
14111412
} else {
14121413
clangcxx_log_error("unfound arg type: {}", Ty.getAsString());
14131414
}
1415+
return nullptr;
1416+
};
1417+
luisa::vector<const clang::VarDecl*> input_params(params.begin(), params.end());
1418+
if (is_kernel) {
1419+
for (const auto *var : db->extern_vars) {
1420+
collect_arg(var);
1421+
}
1422+
}
1423+
for (auto param : params) {
1424+
collect_arg(param);
14141425
}
14151426

14161427
// ctor initializers
@@ -1468,6 +1479,17 @@ void RecordDeclStmtHandler::run(const MatchFinder::MatchResult &Result) {
14681479
}
14691480
}
14701481

1482+
void ExternVarHandler::run(const MatchFinder::MatchResult &Result) {
1483+
if (const auto *S = Result.Nodes.getNodeAs<clang::VarDecl>("VarDecl")) {
1484+
bool ignore = false;
1485+
for (auto Anno : S->specific_attrs<clang::AnnotateAttr>())
1486+
ignore |= isIgnore(Anno);
1487+
if (!ignore && S->hasExternalStorage()) {
1488+
db->extern_vars.emplace_back(S);
1489+
}
1490+
}
1491+
}
1492+
14711493
void GlobalVarHandler::run(const MatchFinder::MatchResult &Result) {
14721494
if (const auto *S = Result.Nodes.getNodeAs<clang::VarDecl>("VarDecl")) {
14731495
bool ignore = false;
@@ -1545,6 +1567,13 @@ ASTConsumerBase::ASTConsumerBase() {
15451567
unless(isExpansionInSystemHeader()))
15461568
.bind("VarDecl"),
15471569
&HandlerForGlobalVar);
1570+
1571+
HandlerForExternlVar.db = &db;
1572+
Matcher.addMatcher(varDecl(
1573+
unless(isDefinition()),
1574+
unless(isExpansionInSystemHeader()))
1575+
.bind("VarDecl"),
1576+
&HandlerForExternlVar);
15481577
}
15491578

15501579
ASTConsumerBase::~ASTConsumerBase() {

src/clangcxx/src/llvm/ASTConsumer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ struct GlobalVarHandler : public clang::ast_matchers::MatchFinder::MatchCallback
7272
TypeDatabase *db = nullptr;
7373
};
7474

75+
struct ExternVarHandler : public clang::ast_matchers::MatchFinder::MatchCallback {
76+
ExternVarHandler() = default;
77+
void run(const MatchFinder::MatchResult &Result) final;
78+
79+
TypeDatabase *db = nullptr;
80+
};
81+
7582
struct FunctionDeclStmtHandler : public clang::ast_matchers::MatchFinder::MatchCallback {
7683
FunctionDeclStmtHandler() = default;
7784
void run(const MatchFinder::MatchResult &Result) final;
@@ -89,6 +96,7 @@ class ASTConsumerBase : public clang::ASTConsumer {
8996
TypeDatabase db;
9097
RecordDeclStmtHandler HandlerForTypeDecl;
9198
GlobalVarHandler HandlerForGlobalVar;
99+
ExternVarHandler HandlerForExternlVar;
92100
FunctionDeclStmtHandler HandlerForFuncionDecl;
93101
clang::ast_matchers::MatchFinder Matcher;
94102
};

src/clangcxx/src/llvm/TypeDatabase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct TypeDatabase {
4040
void DumpWithLocation(const clang::Decl *decl);
4141

4242
// luisa::unordered_map<luisa::string, const luisa::compute::RefExpr *> globals;
43+
luisa::vector<const clang::VarDecl*> extern_vars;
4344
luisa::shared_ptr<compute::detail::FunctionBuilder> kernel_builder;
4445
luisa::shared_ptr<compute::detail::FunctionBuilder> vertex_builder;
4546
luisa::shared_ptr<compute::detail::FunctionBuilder> pixel_builder;

0 commit comments

Comments
 (0)