[llvm] Prevent negative index in VRMap to avoid segfault (PR #121926)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 7 04:55:01 PST 2025


https://github.com/saharr1234 created https://github.com/llvm/llvm-project/pull/121926

When computing ReuseStage - np in generateExistingPhis, there's a possibility that this expression becomes negative, causing out-of-bounds access in VRMap. This patch adds a boundary check to ensure the index remains valid, preventing a segmentation fault in corner cases.

>From 61e9ae57ceddffdeed79899334d6f1f5a389b8ce Mon Sep 17 00:00:00 2001
From: saharr <sahar.rofe at mobileye.com>
Date: Tue, 7 Jan 2025 14:23:32 +0200
Subject: [PATCH] Prevent negative index in VRMap to avoid segfault

When computing ReuseStage - np in generateExistingPhis, there's a possibility
that this expression becomes negative, causing out-of-bounds access in VRMap.
This patch adds a boundary check to ensure the index remains valid, preventing
a segmentation fault in corner cases.
---
 llvm/lib/CodeGen/ModuloSchedule.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/ModuloSchedule.cpp b/llvm/lib/CodeGen/ModuloSchedule.cpp
index 414c8cd71809db..c11751d177feea 100644
--- a/llvm/lib/CodeGen/ModuloSchedule.cpp
+++ b/llvm/lib/CodeGen/ModuloSchedule.cpp
@@ -527,7 +527,7 @@ void ModuloScheduleExpander::generateExistingPhis(
               ReuseStage -= LVNumStages;
             // Check if the Phi to reuse has been generated yet. If not, then
             // there is nothing to reuse.
-            if (VRMap[ReuseStage - np].count(LoopVal)) {
+            if (ReuseStage >= np && VRMap[ReuseStage - np].count(LoopVal)) {
               NewReg = VRMap[ReuseStage - np][LoopVal];
 
               rewriteScheduledInstr(NewBB, InstrMap, CurStageNum, np, &*BBI,



More information about the llvm-commits mailing list