[flang-commits] [flang] [flang][CUDA] Fix crash in name resolution for CUDA function (PR #108616)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Fri Sep 13 10:52:13 PDT 2024
https://github.com/klausler created https://github.com/llvm/llvm-project/pull/108616
When a function result type appears on a FUNCTION statement after some CUDA attributes, there wasn't always valid program source location information attached to the function result variable information stack. Ensure that some relevant source information is always available.
>From a99edba7274e180238c37810aa1c2f44097acef9 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Fri, 13 Sep 2024 10:49:49 -0700
Subject: [PATCH] [flang][CUDA] Fix crash in name resolution for CUDA function
When a function result type appears on a FUNCTION statement after
some CUDA attributes, there wasn't always valid program source location
information attached to the function result variable information
stack. Ensure that some relevant source information is always
available.
---
flang/lib/Semantics/resolve-names.cpp | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index d8f601212d8d01..b99f308e1c7fab 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -474,15 +474,15 @@ class FuncResultStack {
~FuncResultStack();
struct FuncInfo {
- explicit FuncInfo(const Scope &s) : scope{s} {}
+ FuncInfo(const Scope &s, SourceName at) : scope{s}, source{at} {}
const Scope &scope;
+ SourceName source;
// Parse tree of the type specification in the FUNCTION prefix
const parser::DeclarationTypeSpec *parsedType{nullptr};
// Name of the function RESULT in the FUNCTION suffix, if any
const parser::Name *resultName{nullptr};
// Result symbol
Symbol *resultSymbol{nullptr};
- std::optional<SourceName> source;
bool inFunctionStmt{false}; // true between Pre/Post of FunctionStmt
};
@@ -492,7 +492,9 @@ class FuncResultStack {
void CompleteTypeIfFunctionResult(Symbol &);
FuncInfo *Top() { return stack_.empty() ? nullptr : &stack_.back(); }
- FuncInfo &Push(const Scope &scope) { return stack_.emplace_back(scope); }
+ FuncInfo &Push(const Scope &scope, SourceName at) {
+ return stack_.emplace_back(scope, at);
+ }
void Pop();
private:
@@ -3799,11 +3801,13 @@ bool SubprogramVisitor::Pre(const parser::PrefixSpec &x) {
if (const auto *parsedType{std::get_if<parser::DeclarationTypeSpec>(&x.u)}) {
if (FuncResultStack::FuncInfo * info{funcResultStack().Top()}) {
if (info->parsedType) { // C1543
- Say(currStmtSource().value(),
+ Say(currStmtSource().value_or(info->source),
"FUNCTION prefix cannot specify the type more than once"_err_en_US);
} else {
info->parsedType = parsedType;
- info->source = currStmtSource();
+ if (auto at{currStmtSource()}) {
+ info->source = *at;
+ }
}
} else {
Say(currStmtSource().value(),
@@ -3978,6 +3982,9 @@ bool SubprogramVisitor::Pre(const parser::FunctionStmt &) {
FuncResultStack::FuncInfo &info{DEREF(funcResultStack().Top())};
CHECK(!info.inFunctionStmt);
info.inFunctionStmt = true;
+ if (auto at{currStmtSource()}) {
+ info.source = *at;
+ }
return BeginAttrs();
}
bool SubprogramVisitor::Pre(const parser::EntryStmt &) { return BeginAttrs(); }
@@ -4507,7 +4514,7 @@ Symbol &SubprogramVisitor::PushSubprogramScope(const parser::Name &name,
symbol->set(subpFlag);
PushScope(Scope::Kind::Subprogram, symbol);
if (subpFlag == Symbol::Flag::Function) {
- funcResultStack().Push(currScope());
+ funcResultStack().Push(currScope(), name.source);
}
if (inInterfaceBlock()) {
auto &details{symbol->get<SubprogramDetails>()};
More information about the flang-commits
mailing list