Skip to content

Conversation

@ncelikNV
Copy link
Contributor

Fixes sanitizers silently not being enabled due to broken checks for
compiler support of sanitizer flags.

Fixes non-leak errors reported by ASan when running slang-test -api cpu on Linux.

Fixes #9097.
Fixes #9098.

Clang was taking around 10 minutes to compile
`slang-embedded-core-module-source.cpp` with optimizations. Disabling
optimizations for these functions reduced the compilation time to about
12 seconds on my machine (Intel Core Ultra 7 165H), and had no
noticeable impact on run-time performance.

Run-time performance with optimizations:

```
$ hyperfine --shell=none './build/generators/Release/bin/slang-bootstrap -archive-type riff-lz4 -save-core-module-bin-source slang-core-module-generated.h -save-glsl-module-bin-source slang-glsl-module-generated.h'
Benchmark 1: ./build/generators/Release/bin/slang-bootstrap -archive-type riff-lz4 -save-core-module-bin-source slang-core-module-generated.h -save-glsl-module-bin-source slang-glsl-module-generated.h
  Time (mean ± σ):      2.545 s ±  0.035 s    [User: 2.333 s, System: 0.210 s]
  Range (min … max):    2.496 s …  2.620 s    10 runs
```

Run-time performance without optimizations:

```
$ hyperfine --shell=none './build/generators/Release/bin/slang-bootstrap -archive-type riff-lz4 -save-core-module-bin-source slang-core-module-generated.h -save-glsl-module-bin-source slang-glsl-module-generated.h'
Benchmark 1: ./build/generators/Release/bin/slang-bootstrap -archive-type riff-lz4 -save-core-module-bin-source slang-core-module-generated.h -save-glsl-module-bin-source slang-glsl-module-generated.h
  Time (mean ± σ):      2.564 s ±  0.039 s    [User: 2.350 s, System: 0.213 s]
  Range (min … max):    2.512 s …  2.614 s    10 runs
```

Disabling optimizations also makes
`slang-embedded-core-module-source.cpp.o` slightly smaller:

- 7.84 MiB with optimizations,
- 7.37 MiB without optimizations.

Fixes shader-slang#9054.
@ncelikNV ncelikNV added the pr: non-breaking PRs without breaking changes label Nov 24, 2025
@slangbot
Copy link
Contributor

slangbot commented Nov 24, 2025

⚠️ Serialization Code Changed

This PR modifies source/slang/slang-serialize-ir.cpp. Please review if you need to update:

  • kSupportedSerializationVersion: Should be incremented if you're making backwards-incompatible changes to the serialization format

This version number helps maintain compatibility when loading serialized IR modules.

target_compile_options(${target} PRIVATE /fsanitize=address)
target_link_options(${target} PRIVATE /INCREMENTAL:NO)
else()
message(FATAL_ERROR "SLANG_ENABLE_ASAN: unsupported C++ compiler")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be better to raise a FATAL_ERROR (which stops CMake generation) as users might not notice a warning. Since SLANG_ENABLE_ASAN defaults to OFF I think it's safe to assume that it being ON implies the user really doesn't want Slang being built without sanitizers.

Comment on lines +623 to +627
if (PlatformUtil::isFamily(PlatformFamily::Unix, platformKind))
{
// Position independent
cmdLine.addArg("-fPIC");
}
Copy link
Contributor Author

@ncelikNV ncelikNV Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial discussion: https://github.com/shader-slang/slang/pull/8980/files#r2528348258.

GCC used to emit the following warning when using -fPIC on Windows until GCC 6.1 (released in 2016):

-fPIC ignored for target (all code is position independent)

See gcc-mirror/gcc@0a1d992. I assume that's why this was restricted to PlatformFamily::Unix. The commit that added this restriction is from 2019 (~3 years after GCC 6.1's release): ea20066.


diagnostics->reset();
diagnostics->setRaw(SliceUtil::asCharSlice(exeRes.standardError));
diagnostics->appendRaw(SliceUtil::asCharSlice(exeRes.standardError));
Copy link
Contributor Author

@ncelikNV ncelikNV Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since CommandLineDownstreamCompiler::compile now splits compilation and linking into two commands, we need to be able to combine multiple commands' output into the same diagnostics object. Note that parseOutput is only ever called on a newly created diagnostics object (even without this PR's changes), so there was no need to call diagnostics->reset() here.

Comment on lines -470 to +474
char* getData() const { return m_buffer ? m_buffer->getData() : (char*)""; }
char* getData() const
{
static char empty[] = "";
return m_buffer ? m_buffer->getData() : empty;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines -661 to +669
bool operator==(const char* strbuffer) const { return (strcmp(begin(), strbuffer) == 0); }
bool operator==(const char* strbuffer) const
{
const char* volatile b = begin();
return (strcmp(b, strbuffer) == 0);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines -1902 to +1903
Fossil::SerialWriter writer(blobBuilder);
ASTSerialWriteContext context(moduleDecl, sourceLocWriter);
Fossil::SerialWriter writer(blobBuilder);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes ASan error: stack-use-after-scope.

Comment on lines -410 to +411
ComPtr<ISlangWriter> writer(new FileWriter(stderr, WriterFlag::AutoFlush));
ComPtr<ISlangWriter> writer(
new FileWriter(stderr, WriterFlag::IsUnowned | WriterFlag::AutoFlush));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stderr was being closed when exiting main, which meant sanitizers would fail to print diagnostics.

Comment on lines 133 to +139
const SlangPassThrough cppCompilers[] = {
SLANG_PASS_THROUGH_VISUAL_STUDIO,
SLANG_PASS_THROUGH_GCC,
#if SLANG_CLANG
SLANG_PASS_THROUGH_CLANG,
#else
SLANG_PASS_THROUGH_GCC,
#endif
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue here was that I was building Slang with Clang but a test was being built with GCC and the sanitizer runtime of one is incompatible with the other's. Maybe this code should just be edited to only test the compiler that is being used to build Slang instead of MSVC + either Clang or GCC, this was more of a quick fix.

Comment on lines -236 to +239
return UnownedStringSlice();
return String();
}

return UnownedStringSlice(fileStat.m_filename).trim('/');
return String(UnownedStringSlice(fileStat.m_filename).trim('/'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes ASan error: stack-use-after-return.

Fixes sanitizers silently not being enabled due to broken checks for
compiler support of sanitizer flags.

Fixes non-leak errors reported by ASan when running `slang-test -api
cpu` on Linux.

Fixes shader-slang#9097.
Fixes shader-slang#9098.
@ncelikNV ncelikNV force-pushed the fix-slang-enable-asan-1 branch from 1cbaf81 to d33b400 Compare November 25, 2025 15:38
SLANG_CHECK_ABORT(code != nullptr);

SLANG_CHECK(code->getBufferSize() != 0);
SLANG_CHECK_ABORT(code->getBufferSize() != 0);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes crash/segfault due to null pointer dereference if libslang-glslang-<version>.so couldn't be found (build/<config>/lib not in LD_LIBRARY_PATH): code would be nullptr.

SLANG_CHECK(compileResult->getMetadata(metadata.writeRef()) == SLANG_OK);
SLANG_CHECK_ABORT(result == SLANG_OK);
SLANG_CHECK_ABORT(compileResult != nullptr);
SLANG_CHECK_ABORT(compileResult->getItemCount() == 2);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes crash/segfault due to null pointer dereference if libslang-glslang-<version>.so couldn't be found (build/<config>/lib not in LD_LIBRARY_PATH): result would be != SLANG_OK and compileResult would be nullptr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: non-breaking PRs without breaking changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix non-leak ASan errors reported during CPU-only testing on Linux Fix SLANG_ENABLE_ASAN CMake option

2 participants