Skip to content

Commit eba21ac

Browse files
authored
[SandboxIR][Utils] Implement getMemoryLocation() (#109724)
This patch implements sandboxir::Utils::memoryLocationGetOrNone() that calls MemoryLocation::getOrNone() internally. Ideally this would require a sandboxir::MemoryLocation, but this should be good enough for now.
1 parent 394f59c commit eba21ac

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ class Value {
346346
friend class NoCFIValue; // For `Val`.
347347
friend class ConstantPtrAuth; // For `Val`.
348348
friend class ConstantExpr; // For `Val`.
349+
friend class Utils; // For `Val`.
349350

350351
// Region needs to manipulate metadata in the underlying LLVM Value, we don't
351352
// expose metadata in sandboxir.

llvm/include/llvm/SandboxIR/Utils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#ifndef LLVM_SANDBOXIR_UTILS_H
1313
#define LLVM_SANDBOXIR_UTILS_H
1414

15+
#include "llvm/Analysis/MemoryLocation.h"
16+
#include "llvm/SandboxIR/SandboxIR.h"
17+
1518
namespace llvm::sandboxir {
1619

1720
class Utils {
@@ -48,6 +51,12 @@ class Utils {
4851
Type *Ty = getExpectedType(V);
4952
return DL.getTypeSizeInBits(Ty->LLVMTy);
5053
}
54+
55+
/// Equivalent to MemoryLocation::getOrNone(I).
56+
static std::optional<llvm::MemoryLocation>
57+
memoryLocationGetOrNone(const Instruction *I) {
58+
return llvm::MemoryLocation::getOrNone(cast<llvm::Instruction>(I->Val));
59+
}
5160
};
5261
} // namespace llvm::sandboxir
5362

llvm/lib/SandboxIR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ add_llvm_component_library(LLVMSandboxIR
1111
LINK_COMPONENTS
1212
Core
1313
Support
14+
Analysis
1415
)
1516

llvm/unittests/SandboxIR/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ set(LLVM_LINK_COMPONENTS
22
AsmParser
33
SandboxIR
44
Core
5+
Analysis
56
)
67

78
add_llvm_unittest(SandboxIRTests
89
PassTest.cpp
910
SandboxIRTest.cpp
1011
TrackerTest.cpp
1112
TypesTest.cpp
13+
UtilsTest.cpp
1214
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===- UtilsTest.cpp ------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/SandboxIR/Utils.h"
10+
#include "llvm/AsmParser/Parser.h"
11+
#include "llvm/IR/BasicBlock.h"
12+
#include "llvm/IR/DataLayout.h"
13+
#include "llvm/IR/Function.h"
14+
#include "llvm/IR/Instruction.h"
15+
#include "llvm/IR/Module.h"
16+
#include "llvm/SandboxIR/SandboxIR.h"
17+
#include "llvm/Support/SourceMgr.h"
18+
#include "gtest/gtest.h"
19+
20+
using namespace llvm;
21+
22+
struct UtilsTest : public testing::Test {
23+
LLVMContext C;
24+
std::unique_ptr<Module> M;
25+
26+
void parseIR(LLVMContext &C, const char *IR) {
27+
SMDiagnostic Err;
28+
M = parseAssemblyString(IR, Err, C);
29+
if (!M)
30+
Err.print("UtilsTest", errs());
31+
}
32+
BasicBlock *getBasicBlockByName(Function &F, StringRef Name) {
33+
for (BasicBlock &BB : F)
34+
if (BB.getName() == Name)
35+
return &BB;
36+
llvm_unreachable("Expected to find basic block!");
37+
}
38+
};
39+
40+
TEST_F(UtilsTest, getMemoryLocation) {
41+
parseIR(C, R"IR(
42+
define void @foo(ptr %arg0) {
43+
%ld = load i8, ptr %arg0
44+
ret void
45+
}
46+
)IR");
47+
llvm::Function *LLVMF = &*M->getFunction("foo");
48+
auto *LLVMBB = &*LLVMF->begin();
49+
auto *LLVMLd = cast<llvm::LoadInst>(&*LLVMBB->begin());
50+
sandboxir::Context Ctx(C);
51+
sandboxir::Function *F = Ctx.createFunction(LLVMF);
52+
auto *BB = &*F->begin();
53+
auto *Ld = cast<sandboxir::LoadInst>(&*BB->begin());
54+
EXPECT_EQ(sandboxir::Utils::memoryLocationGetOrNone(Ld),
55+
MemoryLocation::getOrNone(LLVMLd));
56+
}

0 commit comments

Comments
 (0)