-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[TLI] Fix replace-with-veclib crash with invalid arguments #77112
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
paschalis-mpeis
merged 9 commits into
main
from
users/paschalis-mpeis/fix-replace-veclib-crash
Jan 12, 2024
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
85f89de
Pass replace-with-veclib crashes with invalid arguments
paschalis-mpeis 9dfc955
[TLI] Fix replace-with-veclib crashes with invalid arguments.
paschalis-mpeis c93f1c0
Using VFParam.ParamPos to access Types
paschalis-mpeis 39cb353
Addressing reviewers
paschalis-mpeis 407a683
Addressing reviewers (2)
paschalis-mpeis 254025f
Improved tests by comparing last line of stderr.
paschalis-mpeis b4a0b6e
Fix test to work with and without assertions.
paschalis-mpeis e40d082
Addressing reviewers.
paschalis-mpeis 86380ab
Addressing reviewers (2)
paschalis-mpeis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//===--- ReplaceWithVecLibTest.cpp - replace-with-veclib unit tests -------===// | ||
// | ||
// 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/CodeGen/ReplaceWithVeclib.h" | ||
#include "llvm/Analysis/TargetLibraryInfo.h" | ||
#include "llvm/AsmParser/Parser.h" | ||
#include "llvm/IR/LLVMContext.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/Passes/PassBuilder.h" | ||
#include "llvm/Support/SourceMgr.h" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
|
||
static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) { | ||
SMDiagnostic Err; | ||
std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C); | ||
if (!Mod) | ||
Err.print("ReplaceWithVecLibTest", errs()); | ||
return Mod; | ||
} | ||
|
||
/// Runs ReplaceWithVecLib with different TLIIs that have custom VecDescs. This | ||
/// allows checking that the pass won't crash when the function to replace (from | ||
/// the input IR) does not match the replacement function (derived from the | ||
/// VecDesc mapping). | ||
class ReplaceWithVecLibTest : public ::testing::Test { | ||
protected: | ||
LLVMContext Ctx; | ||
|
||
/// Creates TLII using the given \p VD, and then runs the ReplaceWithVeclib | ||
/// pass. The pass should not crash even when the replacement function | ||
/// (derived from the \p VD mapping) does not match the function to be | ||
/// replaced (from the input \p IR). | ||
bool run(const VecDesc &VD, const char *IR) { | ||
// Create TLII and register it with FAM so it's preserved when | ||
// ReplaceWithVeclib pass runs. | ||
TargetLibraryInfoImpl TLII = TargetLibraryInfoImpl(Triple()); | ||
TLII.addVectorizableFunctions({VD}); | ||
FunctionAnalysisManager FAM; | ||
FAM.registerPass([&TLII]() { return TargetLibraryAnalysis(TLII); }); | ||
|
||
// Register and run the pass on the 'foo' function from the input IR. | ||
FunctionPassManager FPM; | ||
FPM.addPass(ReplaceWithVeclib()); | ||
std::unique_ptr<Module> M = parseIR(Ctx, IR); | ||
PassBuilder PB; | ||
PB.registerFunctionAnalyses(FAM); | ||
FPM.run(*M->getFunction("foo"), FAM); | ||
|
||
return true; | ||
} | ||
}; | ||
|
||
} // end anonymous namespace | ||
|
||
static const char *IR = R"IR( | ||
define <vscale x 4 x float> @foo(<vscale x 4 x float> %in){ | ||
%call = call <vscale x 4 x float> @llvm.powi.f32.i32(<vscale x 4 x float> %in, i32 3) | ||
ret <vscale x 4 x float> %call | ||
} | ||
|
||
declare <vscale x 4 x float> @llvm.powi.f32.i32(<vscale x 4 x float>, i32) #0 | ||
)IR"; | ||
|
||
// The VFABI prefix in TLI describes signature which is matching the powi | ||
// intrinsic declaration. | ||
TEST_F(ReplaceWithVecLibTest, TestValidMapping) { | ||
VecDesc CorrectVD = {"llvm.powi.f32.i32", "_ZGVsMxvu_powi", | ||
ElementCount::getScalable(4), /*Masked*/ true, | ||
"_ZGVsMxvu"}; | ||
EXPECT_TRUE(run(CorrectVD, IR)); | ||
} | ||
|
||
// The VFABI prefix in TLI describes signature which is not matching the powi | ||
// intrinsic declaration. | ||
TEST_F(ReplaceWithVecLibTest, TestInvalidMapping) { | ||
VecDesc IncorrectVD = {"llvm.powi.f32.i32", "_ZGVsMxvv_powi", | ||
ElementCount::getScalable(4), /*Masked*/ true, | ||
"_ZGVsMxvv"}; | ||
EXPECT_TRUE(run(IncorrectVD, IR)); | ||
paschalis-mpeis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.