From 0cea6e682190f502c5daa10d0cf3d27eb3a2d6b2 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Thu, 24 Oct 2024 17:42:28 +0200 Subject: [PATCH] Ensure !NDEBUG with LLVM_ENABLE_ABI_BREAKING_CHECKS does not segfault In SelectionDAG, `TargetTransformInfo::hasBranchDivergence()` can be called when both `NDEBUG` and `LLVM_ENABLE_ABI_BREAKING_CHECKS` are enabled. In that case, the class member `TTI` is still initialized to `nullptr`, causing a segfault. Fix this by ensuring that all the calls to `hasBranchDivergence` and `VerifyDAGDivergence` only occur when `NDEBUG` is disabled, and `LLVM_ENABLE_ABI_BREAKING_CHECKS` is enabled. --- llvm/include/llvm/CodeGen/SelectionDAG.h | 2 +- llvm/include/llvm/CodeGen/SelectionDAGISel.h | 2 +- llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 2 +- .../CodeGen/SelectionDAG/SelectionDAGISel.cpp | 18 +++++++++--------- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/llvm/include/llvm/CodeGen/SelectionDAG.h b/llvm/include/llvm/CodeGen/SelectionDAG.h index 12ff36c89e33e..e82bdb6906163 100644 --- a/llvm/include/llvm/CodeGen/SelectionDAG.h +++ b/llvm/include/llvm/CodeGen/SelectionDAG.h @@ -584,7 +584,7 @@ class SelectionDAG { return Root; } -#if LLVM_ENABLE_ABI_BREAKING_CHECKS +#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS void VerifyDAGDivergence(); #endif diff --git a/llvm/include/llvm/CodeGen/SelectionDAGISel.h b/llvm/include/llvm/CodeGen/SelectionDAGISel.h index f6191c6fdb7fe..32b2ea48179f4 100644 --- a/llvm/include/llvm/CodeGen/SelectionDAGISel.h +++ b/llvm/include/llvm/CodeGen/SelectionDAGISel.h @@ -56,7 +56,7 @@ class SelectionDAGISel { AssumptionCache *AC = nullptr; GCFunctionInfo *GFI = nullptr; SSPLayoutInfo *SP = nullptr; -#if LLVM_ENABLE_ABI_BREAKING_CHECKS +#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS TargetTransformInfo *TTI = nullptr; #endif CodeGenOptLevel OptLevel; diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index 55cebc28e4927..0770355ec18c0 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -11744,7 +11744,7 @@ void SelectionDAG::CreateTopologicalOrder(std::vector &Order) { } } -#if LLVM_ENABLE_ABI_BREAKING_CHECKS +#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS void SelectionDAG::VerifyDAGDivergence() { std::vector TopoOrder; CreateTopologicalOrder(TopoOrder); diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index 9bd894dd952bd..981ab18b59c1c 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -940,7 +940,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() { << "'\n"; CurDAG->dump()); -#if LLVM_ENABLE_ABI_BREAKING_CHECKS +#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS if (TTI->hasBranchDivergence()) CurDAG->VerifyDAGDivergence(); #endif @@ -960,7 +960,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() { << "'\n"; CurDAG->dump()); -#if LLVM_ENABLE_ABI_BREAKING_CHECKS +#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS if (TTI->hasBranchDivergence()) CurDAG->VerifyDAGDivergence(); #endif @@ -982,7 +982,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() { << "'\n"; CurDAG->dump()); -#if LLVM_ENABLE_ABI_BREAKING_CHECKS +#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS if (TTI->hasBranchDivergence()) CurDAG->VerifyDAGDivergence(); #endif @@ -1006,7 +1006,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() { << "'\n"; CurDAG->dump()); -#if LLVM_ENABLE_ABI_BREAKING_CHECKS +#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS if (TTI->hasBranchDivergence()) CurDAG->VerifyDAGDivergence(); #endif @@ -1024,7 +1024,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() { << "'\n"; CurDAG->dump()); -#if LLVM_ENABLE_ABI_BREAKING_CHECKS +#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS if (TTI->hasBranchDivergence()) CurDAG->VerifyDAGDivergence(); #endif @@ -1040,7 +1040,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() { << "'\n"; CurDAG->dump()); -#if LLVM_ENABLE_ABI_BREAKING_CHECKS +#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS if (TTI->hasBranchDivergence()) CurDAG->VerifyDAGDivergence(); #endif @@ -1060,7 +1060,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() { << "'\n"; CurDAG->dump()); -#if LLVM_ENABLE_ABI_BREAKING_CHECKS +#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS if (TTI->hasBranchDivergence()) CurDAG->VerifyDAGDivergence(); #endif @@ -1080,7 +1080,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() { << "'\n"; CurDAG->dump()); -#if LLVM_ENABLE_ABI_BREAKING_CHECKS +#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS if (TTI->hasBranchDivergence()) CurDAG->VerifyDAGDivergence(); #endif @@ -1100,7 +1100,7 @@ void SelectionDAGISel::CodeGenAndEmitDAG() { << "'\n"; CurDAG->dump()); -#if LLVM_ENABLE_ABI_BREAKING_CHECKS +#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS if (TTI->hasBranchDivergence()) CurDAG->VerifyDAGDivergence(); #endif