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:
SetUp()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 
TEST_F(VFABIAttrTest,Write)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 StringRef S = CI->getAttribute(AttributeList::FunctionIndex,
50                                        "vector-function-abi-variant")
51                           .getValueAsString();
52   EXPECT_EQ(S, "_ZGVnN8v_g,_ZGVnN2v_g(custom_vg)");
53 }
54