llvm-project/polly/lib/Support/ScopLocation.cpp
Stephen Tozer b5a273a1cf
[Polly][DebugInfo] Use getStableDebugLoc to avoid intrinsic-dependent behaviour (#81246)
Polly currently uses `getDebugLoc` in a few places to produce diagnostic
output; this is correct when interacting with specific instructions, but
may be incorrect when dealing with instruction ranges if debug
intrinsics are included. As a general rule, the debug locations attached
to debug intrinsics may be misleading compared to the surrounding
instructions, and are not generally used for anything other than
determining variable scope info; the recommended approach is therefore
to use `getStableDebugLoc` instead, which skips over debug intrinsics.
This is necessary to fix test failures that occur when enabling
non-instruction debug info, which removes debug intrinsics from basic
blocks and thus alters the diagnostic output of Polly (despite causing
no functional change).
2024-02-09 12:02:59 +00:00

44 lines
1.3 KiB
C++

//=== ScopLocation.cpp - Debug location for ScopDetection ----- -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Helper function for extracting region debug information.
//
//===----------------------------------------------------------------------===//
//
#include "polly/Support/ScopLocation.h"
#include "llvm/Analysis/RegionInfo.h"
#include "llvm/IR/DebugInfoMetadata.h"
using namespace llvm;
namespace polly {
void getDebugLocation(const Region *R, unsigned &LineBegin, unsigned &LineEnd,
std::string &FileName) {
LineBegin = -1;
LineEnd = 0;
for (const BasicBlock *BB : R->blocks())
for (const Instruction &Inst : *BB) {
DebugLoc DL = Inst.getStableDebugLoc();
if (!DL)
continue;
auto *Scope = cast<DIScope>(DL.getScope());
if (FileName.empty())
FileName = Scope->getFilename().str();
unsigned NewLine = DL.getLine();
LineBegin = std::min(LineBegin, NewLine);
LineEnd = std::max(LineEnd, NewLine);
}
}
} // namespace polly