reader/spirv: Clean up disjoint AST nodes

Perform a program clone at the end of parsing to remove any unreachable AST nodes.
Actually fixing the parser to never create these looks like a huge amount of work.

Fixed: tint:749
Change-Id: Ib956634257e0933c9702d6be22f79942f7cf4c51
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/49520
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2021-04-30 10:43:19 +00:00 committed by Commit Bot service account
parent 9481156eb9
commit 275eb7e4e4
3 changed files with 1897 additions and 7 deletions

View File

@ -25,12 +25,22 @@ namespace spirv {
Program Parse(const std::vector<uint32_t>& input) {
ParserImpl parser(input);
bool parsed = parser.Parse();
ProgramBuilder builder = std::move(parser.builder());
ProgramBuilder& builder = parser.builder();
if (!parsed) {
// TODO(bclayton): Migrate spirv::ParserImpl to using diagnostics.
builder.Diagnostics().add_error(parser.error());
return Program(std::move(builder));
}
return Program(std::move(builder));
// The SPIR-V parser can construct disjoint AST nodes, which is invalid for
// the Resolver. Clone the Program to clean these up.
builder.SetResolveOnBuild(false);
Program program_with_disjoint_ast(std::move(builder));
ProgramBuilder output;
CloneContext(&output, &program_with_disjoint_ast, false).Clone();
return Program(std::move(output));
}
} // namespace spirv

1851
test/bug_tint_749.spvasm Normal file

File diff suppressed because it is too large Load Diff

View File

@ -16,6 +16,7 @@
set -e # Fail on any error.
TEXT_YELLOW="\033[0;33m"
TEXT_GREEN="\033[0;32m"
TEXT_RED="\033[0;31m"
TEXT_DEFAULT="\033[0m"
@ -33,15 +34,42 @@ fi
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )"
NUM_PASS=0
NUM_SKIP=0
NUM_FAIL=0
SKIPPED=""
SKIPPED+="msl:bug_tint_749.spvasm" # TINT_UNIMPLEMENTED crbug.com/tint/726: module-scope private and workgroup variables not yet implemented
SKIPPED+="hlsl:bug_tint_749.spvasm" # Failed to generate: error: pointers not supported in HLSL
# should_skip(TEST_FILE, FORMAT)
function should_skip() {
local TEST="$1-$2"
if [[ "$TEST" == "bug_tint_749.spvasm-msl" ]]; then
echo 1
return
fi
echo 0
return
}
# check(TEST_FILE, FORMAT)
function check() {
TEST_FILE=$1
FORMAT=$2
local TEST_FILE=$1
local FORMAT=$2
SKIP=
if [[ $SKIPPED == *"${FORMAT}:${TEST_FILE}"* ]]; then
SKIP=1
fi
printf "%7s: " "${FORMAT}"
if [[ -n "$SKIP" ]]; then
echo -e "${TEXT_YELLOW}SKIPPED${TEXT_DEFAULT}"
NUM_SKIP=$((${NUM_SKIP}+1))
return
fi
set +e
${TINT} ${TEST_FILE} --format ${FORMAT} -o /dev/null
${TINT} ${SCRIPT_DIR}/${TEST_FILE} --format ${FORMAT} -o /dev/null
if [ $? -eq 0 ]; then
echo -e "${TEXT_GREEN}PASS${TEXT_DEFAULT}"
NUM_PASS=$((${NUM_PASS}+1))
@ -54,6 +82,7 @@ function check() {
for TEST_FILE in ${SCRIPT_DIR}/*.spvasm ${SCRIPT_DIR}/*.wgsl
do
TEST_FILE=$(realpath --relative-to="$SCRIPT_DIR" "$TEST_FILE")
echo
echo "Testing $TEST_FILE..."
check "${TEST_FILE}" wgsl
@ -64,11 +93,11 @@ done
if [ ${NUM_FAIL} -ne 0 ]; then
echo
echo -e "${TEXT_RED}${NUM_FAIL} tests failed. ${TEXT_DEFAULT}${NUM_PASS} passed${TEXT_DEFAULT}"
echo -e "${TEXT_RED}${NUM_FAIL} tests failed. ${TEXT_DEFAULT}${NUM_SKIP} skipped. ${NUM_PASS} passed.${TEXT_DEFAULT}"
echo
exit 1
else
echo
echo -e "${TEXT_GREEN}All ${NUM_PASS} tests pass${TEXT_DEFAULT}"
echo -e "${NUM_SKIP} tests skipped. ${TEXT_GREEN}${NUM_PASS} passed.${TEXT_DEFAULT}"
echo
fi