[llvm] eba21ac - [SandboxIR][Utils] Implement getMemoryLocation() (#109724)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 25 10:43:39 PDT 2024
Author: vporpo
Date: 2024-09-25T10:43:36-07:00
New Revision: eba21accf221c16875f1318952a0b1f468913a30
URL: https://github.com/llvm/llvm-project/commit/eba21accf221c16875f1318952a0b1f468913a30
DIFF: https://github.com/llvm/llvm-project/commit/eba21accf221c16875f1318952a0b1f468913a30.diff
LOG: [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.
Added:
llvm/unittests/SandboxIR/UtilsTest.cpp
Modified:
llvm/include/llvm/SandboxIR/SandboxIR.h
llvm/include/llvm/SandboxIR/Utils.h
llvm/lib/SandboxIR/CMakeLists.txt
llvm/unittests/SandboxIR/CMakeLists.txt
Removed:
################################################################################
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index d4c907ce8327dd..d99d564ba24e5c 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -346,6 +346,7 @@ class Value {
friend class NoCFIValue; // For `Val`.
friend class ConstantPtrAuth; // For `Val`.
friend class ConstantExpr; // For `Val`.
+ friend class Utils; // For `Val`.
// Region needs to manipulate metadata in the underlying LLVM Value, we don't
// expose metadata in sandboxir.
diff --git a/llvm/include/llvm/SandboxIR/Utils.h b/llvm/include/llvm/SandboxIR/Utils.h
index ccc0030868a55b..4e8a175f547059 100644
--- a/llvm/include/llvm/SandboxIR/Utils.h
+++ b/llvm/include/llvm/SandboxIR/Utils.h
@@ -12,6 +12,9 @@
#ifndef LLVM_SANDBOXIR_UTILS_H
#define LLVM_SANDBOXIR_UTILS_H
+#include "llvm/Analysis/MemoryLocation.h"
+#include "llvm/SandboxIR/SandboxIR.h"
+
namespace llvm::sandboxir {
class Utils {
@@ -48,6 +51,12 @@ class Utils {
Type *Ty = getExpectedType(V);
return DL.getTypeSizeInBits(Ty->LLVMTy);
}
+
+ /// Equivalent to MemoryLocation::getOrNone(I).
+ static std::optional<llvm::MemoryLocation>
+ memoryLocationGetOrNone(const Instruction *I) {
+ return llvm::MemoryLocation::getOrNone(cast<llvm::Instruction>(I->Val));
+ }
};
} // namespace llvm::sandboxir
diff --git a/llvm/lib/SandboxIR/CMakeLists.txt b/llvm/lib/SandboxIR/CMakeLists.txt
index 03474be0c7b80a..b2e6f6285fea5a 100644
--- a/llvm/lib/SandboxIR/CMakeLists.txt
+++ b/llvm/lib/SandboxIR/CMakeLists.txt
@@ -11,5 +11,6 @@ add_llvm_component_library(LLVMSandboxIR
LINK_COMPONENTS
Core
Support
+ Analysis
)
diff --git a/llvm/unittests/SandboxIR/CMakeLists.txt b/llvm/unittests/SandboxIR/CMakeLists.txt
index a228637b062a43..2ab284a511fcaa 100644
--- a/llvm/unittests/SandboxIR/CMakeLists.txt
+++ b/llvm/unittests/SandboxIR/CMakeLists.txt
@@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS
AsmParser
SandboxIR
Core
+ Analysis
)
add_llvm_unittest(SandboxIRTests
@@ -9,4 +10,5 @@ add_llvm_unittest(SandboxIRTests
SandboxIRTest.cpp
TrackerTest.cpp
TypesTest.cpp
+ UtilsTest.cpp
)
diff --git a/llvm/unittests/SandboxIR/UtilsTest.cpp b/llvm/unittests/SandboxIR/UtilsTest.cpp
new file mode 100644
index 00000000000000..ded3edf1206a4b
--- /dev/null
+++ b/llvm/unittests/SandboxIR/UtilsTest.cpp
@@ -0,0 +1,56 @@
+//===- UtilsTest.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 "llvm/SandboxIR/Utils.h"
+#include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/DataLayout.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Instruction.h"
+#include "llvm/IR/Module.h"
+#include "llvm/SandboxIR/SandboxIR.h"
+#include "llvm/Support/SourceMgr.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+struct UtilsTest : public testing::Test {
+ LLVMContext C;
+ std::unique_ptr<Module> M;
+
+ void parseIR(LLVMContext &C, const char *IR) {
+ SMDiagnostic Err;
+ M = parseAssemblyString(IR, Err, C);
+ if (!M)
+ Err.print("UtilsTest", errs());
+ }
+ BasicBlock *getBasicBlockByName(Function &F, StringRef Name) {
+ for (BasicBlock &BB : F)
+ if (BB.getName() == Name)
+ return &BB;
+ llvm_unreachable("Expected to find basic block!");
+ }
+};
+
+TEST_F(UtilsTest, getMemoryLocation) {
+ parseIR(C, R"IR(
+define void @foo(ptr %arg0) {
+ %ld = load i8, ptr %arg0
+ ret void
+}
+)IR");
+ llvm::Function *LLVMF = &*M->getFunction("foo");
+ auto *LLVMBB = &*LLVMF->begin();
+ auto *LLVMLd = cast<llvm::LoadInst>(&*LLVMBB->begin());
+ sandboxir::Context Ctx(C);
+ sandboxir::Function *F = Ctx.createFunction(LLVMF);
+ auto *BB = &*F->begin();
+ auto *Ld = cast<sandboxir::LoadInst>(&*BB->begin());
+ EXPECT_EQ(sandboxir::Utils::memoryLocationGetOrNone(Ld),
+ MemoryLocation::getOrNone(LLVMLd));
+}
More information about the llvm-commits
mailing list