Skip to content

Commit efbac49

Browse files
author
git apple-llvm automerger
committed
Merge commit 'a3ea7e10f47d' from apple/master into swift/master-next
2 parents 41bfea0 + a3ea7e1 commit efbac49

File tree

8 files changed

+170
-5
lines changed

8 files changed

+170
-5
lines changed

llvm/include/llvm/Analysis/VectorUtils.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_ANALYSIS_VECTORUTILS_H
1515

1616
#include "llvm/ADT/MapVector.h"
17+
#include "llvm/ADT/SmallSet.h"
1718
#include "llvm/Analysis/LoopAccessAnalysis.h"
1819
#include "llvm/IR/IRBuilder.h"
1920
#include "llvm/Support/CheckedArithmetic.h"
@@ -121,14 +122,20 @@ namespace VFABI {
121122
/// * x86 (libmvec): https://sourceware.org/glibc/wiki/libmvec and
122123
/// https://sourceware.org/glibc/wiki/libmvec?action=AttachFile&do=view&target=VectorABI.txt
123124
///
124-
///
125-
///
126125
/// \param MangledName -> input string in the format
127126
/// _ZGV<isa><mask><vlen><parameters>_<scalarname>[(<redirection>)].
128127
Optional<VFInfo> tryDemangleForVFABI(StringRef MangledName);
129128

130129
/// Retrieve the `VFParamKind` from a string token.
131130
VFParamKind getVFParamKindFromString(const StringRef Token);
131+
132+
// Name of the attribute where the variant mappings are stored.
133+
static constexpr char const *MappingsAttrName = "vector-function-abi-variant";
134+
135+
/// Populates a set of strings representing the Vector Function ABI variants
136+
/// associated to the CallInst CI.
137+
void getVectorVariantNames(const CallInst &CI,
138+
SmallVectorImpl<std::string> &VariantMappings);
132139
} // end namespace VFABI
133140

134141
template <typename T> class ArrayRef;
@@ -137,7 +144,6 @@ class GetElementPtrInst;
137144
template <typename InstTy> class InterleaveGroup;
138145
class Loop;
139146
class ScalarEvolution;
140-
class TargetLibraryInfo;
141147
class TargetTransformInfo;
142148
class Type;
143149
class Value;

llvm/include/llvm/Transforms/Utils/ModuleUtils.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
1414
#define LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
1515

16+
#include "llvm/ADT/SmallSet.h"
1617
#include "llvm/ADT/StringRef.h"
1718
#include <utility> // for std::pair
1819

@@ -108,6 +109,23 @@ void filterDeadComdatFunctions(
108109
/// unique identifier for this module, so we return the empty string.
109110
std::string getUniqueModuleId(Module *M);
110111

112+
class TargetLibraryInfo;
113+
class CallInst;
114+
namespace VFABI {
115+
116+
/// \defgroup Vector Function ABI (VABI) Module functions.
117+
///
118+
/// Utility functions for VFABI data that can modify the module.
119+
///
120+
/// @{
121+
/// Overwrite the Vector Function ABI variants attribute with the names provide
122+
/// in \p VariantMappings.
123+
void setVectorVariantNames(CallInst *CI,
124+
const SmallVector<std::string, 8> &VariantMappings);
125+
126+
/// @}
127+
} // End VFABI namespace
128+
111129
} // End llvm namespace
112130

113131
#endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H

llvm/lib/Analysis/VFABIDemangling.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "llvm/ADT/SmallSet.h"
10+
#include "llvm/ADT/SmallString.h"
911
#include "llvm/Analysis/VectorUtils.h"
1012

1113
using namespace llvm;
@@ -34,7 +36,6 @@ ParseRet tryParseISA(StringRef &MangledName, VFISAKind &ISA) {
3436
.Case("d", VFISAKind::AVX2)
3537
.Case("e", VFISAKind::AVX512)
3638
.Default(VFISAKind::Unknown);
37-
3839
MangledName = MangledName.drop_front(1);
3940

4041
return ParseRet::OK;
@@ -338,7 +339,7 @@ Optional<VFInfo> VFABI::tryDemangleForVFABI(StringRef MangledName) {
338339
}
339340
} while (ParamFound == ParseRet::OK);
340341

341-
// A valid MangledName mus have at least one valid entry in the
342+
// A valid MangledName must have at least one valid entry in the
342343
// <parameters>.
343344
if (Parameters.empty())
344345
return None;

llvm/lib/Analysis/VectorUtils.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,3 +1159,22 @@ void InterleaveGroup<Instruction>::addMetadata(Instruction *NewInst) const {
11591159
propagateMetadata(NewInst, VL);
11601160
}
11611161
}
1162+
1163+
void VFABI::getVectorVariantNames(
1164+
const CallInst &CI, SmallVectorImpl<std::string> &VariantMappings) {
1165+
const StringRef S =
1166+
CI.getAttribute(AttributeList::FunctionIndex, VFABI::MappingsAttrName)
1167+
.getValueAsString();
1168+
SmallVector<StringRef, 8> ListAttr;
1169+
S.split(ListAttr, ",");
1170+
1171+
for (auto &S : SetVector<StringRef>(ListAttr.begin(), ListAttr.end())) {
1172+
#ifndef NDEBUG
1173+
Optional<VFInfo> Info = VFABI::tryDemangleForVFABI(S);
1174+
assert(Info.hasValue() && "Invalid name for a VFABI variant.");
1175+
assert(CI.getModule()->getFunction(Info.getValue().VectorName) &&
1176+
"Vector function is missing.");
1177+
#endif
1178+
VariantMappings.push_back(S);
1179+
}
1180+
}

llvm/lib/Transforms/Utils/ModuleUtils.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/Transforms/Utils/ModuleUtils.h"
14+
#include "llvm/Analysis/VectorUtils.h"
1415
#include "llvm/IR/DerivedTypes.h"
1516
#include "llvm/IR/Function.h"
1617
#include "llvm/IR/IRBuilder.h"
@@ -280,3 +281,31 @@ std::string llvm::getUniqueModuleId(Module *M) {
280281
MD5::stringifyResult(R, Str);
281282
return ("$" + Str).str();
282283
}
284+
285+
void VFABI::setVectorVariantNames(
286+
CallInst *CI, const SmallVector<std::string, 8> &VariantMappings) {
287+
if (VariantMappings.empty())
288+
return;
289+
290+
SmallString<256> Buffer;
291+
llvm::raw_svector_ostream Out(Buffer);
292+
for (const std::string &VariantMapping : VariantMappings)
293+
Out << VariantMapping << ",";
294+
// Get rid of the trailing ','.
295+
assert(!Buffer.str().empty() && "Must have at least one char.");
296+
Buffer.pop_back();
297+
298+
Module *M = CI->getModule();
299+
#ifndef NDEBUG
300+
for (const std::string &VariantMapping : VariantMappings) {
301+
Optional<VFInfo> VI = VFABI::tryDemangleForVFABI(VariantMapping);
302+
assert(VI.hasValue() && "Canno add an invalid VFABI name.");
303+
assert(M->getNamedValue(VI.getValue().VectorName) &&
304+
"Cannot add variant to attribute: "
305+
"vector function declaration is missing.");
306+
}
307+
#endif
308+
CI->addAttribute(
309+
AttributeList::FunctionIndex,
310+
Attribute::get(M->getContext(), MappingsAttrName, Buffer.str()));
311+
}

llvm/unittests/Analysis/VectorFunctionABITest.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/Analysis/VectorUtils.h"
10+
#include "llvm/AsmParser/Parser.h"
11+
#include "llvm/IR/InstIterator.h"
1012
#include "gtest/gtest.h"
1113

1214
using namespace llvm;
@@ -437,3 +439,37 @@ TEST_F(VFABIParserTest, ParseMaskingAVX512) {
437439
EXPECT_EQ(Parameters[1], VFParameter({1, VFParamKind::GlobalPredicate}));
438440
EXPECT_EQ(ScalarName, "sin");
439441
}
442+
443+
class VFABIAttrTest : public testing::Test {
444+
protected:
445+
void SetUp() override {
446+
M = parseAssemblyString(IR, Err, Ctx);
447+
// Get the only call instruction in the block, which is the first
448+
// instruction.
449+
CI = dyn_cast<CallInst>(&*(instructions(M->getFunction("f")).begin()));
450+
}
451+
const char *IR = "define i32 @f(i32 %a) {\n"
452+
" %1 = call i32 @g(i32 %a) #0\n"
453+
" ret i32 %1\n"
454+
"}\n"
455+
"declare i32 @g(i32)\n"
456+
"declare <2 x i32> @custom_vg(<2 x i32>)"
457+
"declare <4 x i32> @_ZGVnN4v_g(<4 x i32>)"
458+
"declare <8 x i32> @_ZGVnN8v_g(<8 x i32>)"
459+
"attributes #0 = { "
460+
"\"vector-function-abi-variant\"=\""
461+
"_ZGVnN2v_g(custom_vg),_ZGVnN4v_g\" }";
462+
LLVMContext Ctx;
463+
SMDiagnostic Err;
464+
std::unique_ptr<Module> M;
465+
CallInst *CI;
466+
SmallVector<std::string, 8> Mappings;
467+
};
468+
469+
TEST_F(VFABIAttrTest, Read) {
470+
VFABI::getVectorVariantNames(*CI, Mappings);
471+
SmallVector<std::string, 8> Exp;
472+
Exp.push_back("_ZGVnN2v_g(custom_vg)");
473+
Exp.push_back("_ZGVnN4v_g");
474+
EXPECT_EQ(Mappings, Exp);
475+
}

llvm/unittests/Transforms/Utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ add_llvm_unittest(UtilsTests
1818
SSAUpdaterBulkTest.cpp
1919
UnrollLoopTest.cpp
2020
ValueMapperTest.cpp
21+
VFABIUtils.cpp
2122
)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===------- VFABIUtils.cpp - VFABI Unittests ----------------------------===//
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/AsmParser/Parser.h"
10+
#include "llvm/IR/InstIterator.h"
11+
#include "llvm/IR/Instructions.h"
12+
#include "llvm/IR/Module.h"
13+
#include "llvm/Support/SourceMgr.h"
14+
#include "llvm/Transforms/Utils/ModuleUtils.h"
15+
#include "gtest/gtest.h"
16+
17+
using namespace llvm;
18+
19+
class VFABIAttrTest : public testing::Test {
20+
protected:
21+
void SetUp() override {
22+
M = parseAssemblyString(IR, Err, Ctx);
23+
// Get the only call instruction in the block, which is the first
24+
// instruction.
25+
CI = dyn_cast<CallInst>(&*(instructions(M->getFunction("f")).begin()));
26+
}
27+
const char *IR = "define i32 @f(i32 %a) {\n"
28+
" %1 = call i32 @g(i32 %a) #0\n"
29+
" ret i32 %1\n"
30+
"}\n"
31+
"declare i32 @g(i32)\n"
32+
"declare <2 x i32> @custom_vg(<2 x i32>)"
33+
"declare <4 x i32> @_ZGVnN4v_g(<4 x i32>)"
34+
"declare <8 x i32> @_ZGVnN8v_g(<8 x i32>)"
35+
"attributes #0 = { "
36+
"\"vector-function-abi-variant\"=\""
37+
"_ZGVnN2v_g(custom_vg),_ZGVnN4v_g\" }";
38+
LLVMContext Ctx;
39+
SMDiagnostic Err;
40+
std::unique_ptr<Module> M;
41+
CallInst *CI;
42+
SmallVector<std::string, 8> Mappings;
43+
};
44+
45+
TEST_F(VFABIAttrTest, Write) {
46+
Mappings.push_back("_ZGVnN8v_g");
47+
Mappings.push_back("_ZGVnN2v_g(custom_vg)");
48+
VFABI::setVectorVariantNames(CI, Mappings);
49+
const AttributeList Attrs = CI->getAttributes();
50+
const AttributeSet FnAttrs = Attrs.getFnAttributes();
51+
const StringRef S = CI->getAttribute(AttributeList::FunctionIndex,
52+
"vector-function-abi-variant")
53+
.getValueAsString();
54+
EXPECT_EQ(S, "_ZGVnN8v_g,_ZGVnN2v_g(custom_vg)");
55+
}

0 commit comments

Comments
 (0)