Files
TheRock/compiler/amd-llvm/llvm/unittests/DebugInfo/DWARF/DWARFExpressionCompactPrinterTest.cpp
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

208 lines
6.2 KiB
C++

//===- llvm/unittest/DebugInfo/DWARFExpressionCompactPrinterTest.cpp ------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "DwarfGenerator.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/DebugInfo/DWARF/DWARFDie.h"
#include "llvm/DebugInfo/DWARF/DWARFExpressionPrinter.h"
#include "llvm/DebugInfo/DWARF/LowLevel/DWARFExpression.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/DataExtractor.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace dwarf;
namespace {
static void appendULEB128(SmallVectorImpl<uint8_t> &V, uint64_t Val) {
uint8_t Buf[16];
unsigned N = encodeULEB128(Val, Buf);
V.append(Buf, Buf + N);
}
class DWARFExpressionCompactPrinterTest : public ::testing::Test {
public:
std::unique_ptr<MCRegisterInfo> MRI;
DWARFExpressionCompactPrinterTest() {
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmPrinters();
Triple TT("armv8a-linux-gnueabi");
std::string ErrorStr;
const Target *TheTarget = TargetRegistry::lookupTarget(TT, ErrorStr);
if (!TheTarget)
return;
MRI.reset(TheTarget->createMCRegInfo(TT));
}
void TestExprPrinter(ArrayRef<uint8_t> ExprData, StringRef Expected);
};
} // namespace
void DWARFExpressionCompactPrinterTest::TestExprPrinter(
ArrayRef<uint8_t> ExprData, StringRef Expected) {
// If we didn't build ARM, do not run the test.
if (!MRI)
GTEST_SKIP();
// Print the expression, passing in the subprogram DIE, and check that the
// result is as expected.
std::string Result;
raw_string_ostream OS(Result);
DataExtractor DE(ExprData, true);
DWARFExpression Expr(DE, 8);
auto GetRegName = [&](uint64_t DwarfRegNum, bool IsEH) -> StringRef {
if (std::optional<MCRegister> LLVMRegNum =
this->MRI->getLLVMRegNum(DwarfRegNum, IsEH))
if (const char *RegName = this->MRI->getName(*LLVMRegNum))
return llvm::StringRef(RegName);
OS << "<unknown register " << DwarfRegNum << ">";
return {};
};
printDwarfExpressionCompact(&Expr, OS, GetRegName);
EXPECT_EQ(OS.str(), Expected);
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_reg0) {
TestExprPrinter({DW_OP_reg0}, "R0");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_reg10) {
TestExprPrinter({DW_OP_reg10}, "R10");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_regx) {
TestExprPrinter({DW_OP_regx, 0x80, 0x02}, "D0");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0) {
TestExprPrinter({DW_OP_breg0, 0x04}, "[R0+4]");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0_large_offset) {
TestExprPrinter({DW_OP_breg0, 0x80, 0x02}, "[R0+256]");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg13) {
TestExprPrinter({DW_OP_breg13, 0x10}, "[SP+16]");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg13_zero_offset) {
TestExprPrinter({DW_OP_breg13, 0x00}, "[SP]");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0_negative) {
TestExprPrinter({DW_OP_breg0, 0x70}, "[R0-16]");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_bregx) {
TestExprPrinter({DW_OP_bregx, 0x0d, 0x28}, "[SP+40]");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_stack_value) {
TestExprPrinter({DW_OP_breg13, 0x04, DW_OP_stack_value}, "SP+4");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_entry_value) {
TestExprPrinter({DW_OP_entry_value, 0x01, DW_OP_reg0, DW_OP_stack_value},
"entry(R0)");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_entry_value_mem) {
TestExprPrinter(
{DW_OP_entry_value, 0x02, DW_OP_breg13, 0x10, DW_OP_stack_value},
"entry([SP+16])");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_nop) {
TestExprPrinter({DW_OP_nop}, "<stack of size 0, expected 1>");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_LLVM_nop) {
TestExprPrinter({DW_OP_LLVM_user, DW_OP_LLVM_nop},
"<stack of size 0, expected 1>");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_nop_OP_reg) {
TestExprPrinter({DW_OP_nop, DW_OP_reg0}, "R0");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_LLVM_nop_OP_reg) {
TestExprPrinter({DW_OP_LLVM_user, DW_OP_LLVM_nop, DW_OP_reg0}, "R0");
}
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_LLVM_user_unknown_subop) {
TestExprPrinter({DW_OP_LLVM_user, DW_OP_LLVM_form_aspace_address},
"<unknown op DW_OP_LLVM_user (233) subop "
"DW_OP_LLVM_form_aspace_address (2)>");
}
// NVPTX encodes virtual register names as a packed uint64_t DWARF register
// number; llvm-dwarfdump must still print the string when MC has no mapping.
TEST(NVPTXPackedRegister, Compact_DW_OP_regx_NoMRI) {
SmallVector<uint8_t, 16> Enc;
Enc.push_back(DW_OP_regx);
appendULEB128(Enc, 0x25726432u);
std::string Result;
raw_string_ostream OS(Result);
DataExtractor DE(Enc, true, 8);
DWARFExpression Expr(DE, 8);
printDwarfExpressionCompact(&Expr, OS, nullptr);
EXPECT_EQ(OS.str(), "%rd2");
}
TEST(NVPTXPackedRegister, Full_DW_OP_regx_NoMRI) {
SmallVector<uint8_t, 16> Enc;
Enc.push_back(DW_OP_regx);
appendULEB128(Enc, 0x25726431u);
std::string Result;
raw_string_ostream OS(Result);
DataExtractor DE(Enc, true, 8);
DWARFExpression Expr(DE, 8);
DIDumpOptions DumpOpts;
printDwarfExpression(&Expr, OS, DumpOpts, nullptr);
EXPECT_EQ(OS.str(), "DW_OP_regx %rd1");
}
TEST(NVPTXPackedRegister, Full_DW_OP_regx_CallbackMiss) {
SmallVector<uint8_t, 16> Enc;
Enc.push_back(DW_OP_regx);
appendULEB128(Enc, 0x25727332u);
std::string Result;
raw_string_ostream OS(Result);
DataExtractor DE(Enc, true, 8);
DWARFExpression Expr(DE, 8);
DIDumpOptions DumpOpts;
DumpOpts.GetNameForDWARFReg = [](uint64_t, bool) -> StringRef { return {}; };
printDwarfExpression(&Expr, OS, DumpOpts, nullptr);
EXPECT_EQ(OS.str(), "DW_OP_regx %rs2");
}