Skip to content

[SandboxIR][Utils] Implement getMemoryLocation() #109724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

/// All values point to the context.
Context &Ctx;
Expand Down
9 changes: 9 additions & 0 deletions llvm/include/llvm/SandboxIR/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/SandboxIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ add_llvm_component_library(LLVMSandboxIR
LINK_COMPONENTS
Core
Support
Analysis
)

2 changes: 2 additions & 0 deletions llvm/unittests/SandboxIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ set(LLVM_LINK_COMPONENTS
AsmParser
SandboxIR
Core
Analysis
)

add_llvm_unittest(SandboxIRTests
PassTest.cpp
SandboxIRTest.cpp
TrackerTest.cpp
TypesTest.cpp
UtilsTest.cpp
)
56 changes: 56 additions & 0 deletions llvm/unittests/SandboxIR/UtilsTest.cpp
Original file line number Diff line number Diff line change
@@ -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!");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably refactor the parseIR/getBasicBlockByName boilerplate into something common for the entire SandboxVectorizer. Every test file duplicates it. But that is a problem for the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that needs to be refactored at some point.

};

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));
}
Loading