Skip to content

[clang][Sema] Optimize ~SemaDiagnosticBuilder #142252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2025
Merged

Conversation

tbaederr
Copy link
Contributor

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 31, 2025
@llvmbot
Copy link
Member

llvmbot commented May 31, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Call getDiagnosticLevel(), which is expensive, only if ShowCallStack is set.

Small gains:
https://llvm-compile-time-tracker.com/compare.php?from=843e362318e884991e517a54446b4faeacdad789&to=5cef0c18466318a426924b3c45f7abaaa8f6b535&stat=instructions:u


Full diff: https://github.com/llvm/llvm-project/pull/142252.diff

1 Files Affected:

  • (modified) clang/lib/Sema/Sema.cpp (+7-4)
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 1901d19b14dfc..ff4c0d833e573 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -2062,11 +2062,14 @@ Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
   if (ImmediateDiag) {
     // Emit our diagnostic and, if it was a warning or error, output a callstack
     // if Fn isn't a priori known-emitted.
-    bool IsWarningOrError = S.getDiagnostics().getDiagnosticLevel(
-                                DiagID, Loc) >= DiagnosticsEngine::Warning;
     ImmediateDiag.reset(); // Emit the immediate diag.
-    if (IsWarningOrError && ShowCallStack)
-      emitCallStackNotes(S, Fn);
+
+    if (ShowCallStack) {
+      bool IsWarningOrError = S.getDiagnostics().getDiagnosticLevel(
+                                  DiagID, Loc) >= DiagnosticsEngine::Warning;
+      if (IsWarningOrError)
+        emitCallStackNotes(S, Fn);
+    }
   } else {
     assert((!PartialDiagId || ShowCallStack) &&
            "Must always show call stack for deferred diags.");

@tbaederr
Copy link
Contributor Author

The original code looks like the ordering with the ImmediateDiag.reset() call matters, but it doesn't seem like it does.

@tbaederr tbaederr merged commit b967561 into llvm:main May 31, 2025
14 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented May 31, 2025

LLVM Buildbot has detected a new failure on builder publish-sphinx-docs running on as-worker-4 while building clang at step 10 "Publish docs-lldb-html".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/45/builds/12825

Here is the relevant piece of the build log for the reference
Step 10 (Publish docs-lldb-html) failure: 'rsync -vrl ...' (failure)
kex_exchange_identification: Connection closed by remote host
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(235) [sender=3.1.3]
Step 11 (Publish docs-flang-html) failure: 'rsync -vrl ...' (failure)
kex_exchange_identification: Connection closed by remote host
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(235) [sender=3.1.3]
Step 12 (Publish docs-openmp-html) failure: 'rsync -vrl ...' (failure)
kex_exchange_identification: Connection closed by remote host
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(235) [sender=3.1.3]
Step 13 (Publish docs-polly-html) failure: 'rsync -vrl ...' (failure)
kex_exchange_identification: Connection closed by remote host
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(235) [sender=3.1.3]

@llvm-ci
Copy link
Collaborator

llvm-ci commented May 31, 2025

LLVM Buildbot has detected a new failure on builder clang-riscv-rva23-evl-vec-2stage running on rise-clang-riscv-rva23-evl-vec-2stage while building clang at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/132/builds/1299

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: '../llvm-zorg/zorg/buildbot/builders/annotated/rise-riscv-build.sh --jobs=16' (failure)
...
PASS: lld :: COFF/ar-comdat.test (81555 of 84675)
PASS: lld :: COFF/arm-thumb-branch20-error.s (81556 of 84675)
PASS: lit :: show-result-codes.py (81557 of 84675)
PASS: lit :: test-output.py (81558 of 84675)
PASS: lld :: COFF/allow-unknown-debug-info.test (81559 of 84675)
PASS: lld :: COFF/alternatename.test (81560 of 84675)
PASS: lld :: COFF/arm64-delayimport.yaml (81561 of 84675)
PASS: lld :: COFF/align.s (81562 of 84675)
PASS: lld :: COFF/arm-thumb-thunks-multipass.s (81563 of 84675)
PASS: lld :: COFF/arm-thumb-thunks.s (81564 of 84675)
FAIL: lit :: timeout-hang.py (81565 of 84675)
******************** TEST 'lit :: timeout-hang.py' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 13
not env -u FILECHECK_OPTS "/usr/bin/python3" /home/buildbot-worker/bbroot/clang-riscv-rva23-evl-vec-2stage/llvm/llvm/utils/lit/lit.py -j1 --order=lexical Inputs/timeout-hang/run-nonexistent.txt  --timeout=1 --param external=0 | "/usr/bin/python3" /home/buildbot-worker/bbroot/clang-riscv-rva23-evl-vec-2stage/stage2/utils/lit/tests/timeout-hang.py 1
# executed command: not env -u FILECHECK_OPTS /usr/bin/python3 /home/buildbot-worker/bbroot/clang-riscv-rva23-evl-vec-2stage/llvm/llvm/utils/lit/lit.py -j1 --order=lexical Inputs/timeout-hang/run-nonexistent.txt --timeout=1 --param external=0
# .---command stderr------------
# | lit.py: /home/buildbot-worker/bbroot/clang-riscv-rva23-evl-vec-2stage/llvm/llvm/utils/lit/lit/main.py:73: note: The test suite configuration requested an individual test timeout of 0 seconds but a timeout of 1 seconds was requested on the command line. Forcing timeout to be 1 seconds.
# `-----------------------------
# executed command: /usr/bin/python3 /home/buildbot-worker/bbroot/clang-riscv-rva23-evl-vec-2stage/stage2/utils/lit/tests/timeout-hang.py 1
# .---command stdout------------
# | Testing took as long or longer than timeout
# `-----------------------------
# error: command failed with exit status: 1

--

********************
PASS: lld :: COFF/arm64-dynamicbase.s (81566 of 84675)
PASS: lld :: COFF/arm64-localimport-align.s (81567 of 84675)
PASS: lld :: COFF/arm64-magic.yaml (81568 of 84675)
PASS: lld :: COFF/arm64-import2.test (81569 of 84675)
PASS: lit :: usage.py (81570 of 84675)
PASS: lld :: COFF/arm64-thunks.s (81571 of 84675)
PASS: lld :: COFF/arm-thumb-thunks-pdb.s (81572 of 84675)
PASS: lld :: COFF/arm64-relocs-imports.test (81573 of 84675)
PASS: lit :: xunit-output.py (81574 of 84675)
PASS: lit :: xunit-output-report-failures-only.py (81575 of 84675)
PASS: lit :: time-tests.py (81576 of 84675)
PASS: lld :: COFF/arm64ec-invalid-name.s (81577 of 84675)
PASS: lld :: COFF/arm64ec-exports.s (81578 of 84675)
PASS: lld :: COFF/arm64ec-loadcfg.s (81579 of 84675)
PASS: lld :: COFF/arm64ec-cust-export-thunk.s (81580 of 84675)
PASS: lit :: use-llvm-tool.py (81581 of 84675)
PASS: lld :: COFF/arm64ec-altnames.s (81582 of 84675)
PASS: lld :: COFF/arm64ec-reloc.test (81583 of 84675)
Step 11 (llvm-project check-all) failure: llvm-project check-all (failure)
...
PASS: lld :: COFF/ar-comdat.test (81555 of 84675)
PASS: lld :: COFF/arm-thumb-branch20-error.s (81556 of 84675)
PASS: lit :: show-result-codes.py (81557 of 84675)
PASS: lit :: test-output.py (81558 of 84675)
PASS: lld :: COFF/allow-unknown-debug-info.test (81559 of 84675)
PASS: lld :: COFF/alternatename.test (81560 of 84675)
PASS: lld :: COFF/arm64-delayimport.yaml (81561 of 84675)
PASS: lld :: COFF/align.s (81562 of 84675)
PASS: lld :: COFF/arm-thumb-thunks-multipass.s (81563 of 84675)
PASS: lld :: COFF/arm-thumb-thunks.s (81564 of 84675)
FAIL: lit :: timeout-hang.py (81565 of 84675)
******************** TEST 'lit :: timeout-hang.py' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 13
not env -u FILECHECK_OPTS "/usr/bin/python3" /home/buildbot-worker/bbroot/clang-riscv-rva23-evl-vec-2stage/llvm/llvm/utils/lit/lit.py -j1 --order=lexical Inputs/timeout-hang/run-nonexistent.txt  --timeout=1 --param external=0 | "/usr/bin/python3" /home/buildbot-worker/bbroot/clang-riscv-rva23-evl-vec-2stage/stage2/utils/lit/tests/timeout-hang.py 1
# executed command: not env -u FILECHECK_OPTS /usr/bin/python3 /home/buildbot-worker/bbroot/clang-riscv-rva23-evl-vec-2stage/llvm/llvm/utils/lit/lit.py -j1 --order=lexical Inputs/timeout-hang/run-nonexistent.txt --timeout=1 --param external=0
# .---command stderr------------
# | lit.py: /home/buildbot-worker/bbroot/clang-riscv-rva23-evl-vec-2stage/llvm/llvm/utils/lit/lit/main.py:73: note: The test suite configuration requested an individual test timeout of 0 seconds but a timeout of 1 seconds was requested on the command line. Forcing timeout to be 1 seconds.
# `-----------------------------
# executed command: /usr/bin/python3 /home/buildbot-worker/bbroot/clang-riscv-rva23-evl-vec-2stage/stage2/utils/lit/tests/timeout-hang.py 1
# .---command stdout------------
# | Testing took as long or longer than timeout
# `-----------------------------
# error: command failed with exit status: 1

--

********************
PASS: lld :: COFF/arm64-dynamicbase.s (81566 of 84675)
PASS: lld :: COFF/arm64-localimport-align.s (81567 of 84675)
PASS: lld :: COFF/arm64-magic.yaml (81568 of 84675)
PASS: lld :: COFF/arm64-import2.test (81569 of 84675)
PASS: lit :: usage.py (81570 of 84675)
PASS: lld :: COFF/arm64-thunks.s (81571 of 84675)
PASS: lld :: COFF/arm-thumb-thunks-pdb.s (81572 of 84675)
PASS: lld :: COFF/arm64-relocs-imports.test (81573 of 84675)
PASS: lit :: xunit-output.py (81574 of 84675)
PASS: lit :: xunit-output-report-failures-only.py (81575 of 84675)
PASS: lit :: time-tests.py (81576 of 84675)
PASS: lld :: COFF/arm64ec-invalid-name.s (81577 of 84675)
PASS: lld :: COFF/arm64ec-exports.s (81578 of 84675)
PASS: lld :: COFF/arm64ec-loadcfg.s (81579 of 84675)
PASS: lld :: COFF/arm64ec-cust-export-thunk.s (81580 of 84675)
PASS: lit :: use-llvm-tool.py (81581 of 84675)
PASS: lld :: COFF/arm64ec-altnames.s (81582 of 84675)
PASS: lld :: COFF/arm64ec-reloc.test (81583 of 84675)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants