xref: /llvm-project/flang/unittests/Optimizer/FIRContextTest.cpp (revision b07ef9e7cd6f5348df0a4f63e70a60491427ff64)
1 //===- FIRContextTest.cpp -------------------------------------------------===//
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 "flang/Optimizer/Dialect/Support/FIRContext.h"
10 #include "gtest/gtest.h"
11 #include "mlir/IR/BuiltinAttributes.h"
12 #include "mlir/IR/BuiltinOps.h"
13 #include "flang/Optimizer/Dialect/Support/KindMapping.h"
14 #include "llvm/TargetParser/Host.h"
15 #include <string>
16 
17 using namespace fir;
18 
19 struct StringAttributesTests : public testing::Test {
20 public:
21   void SetUp() {
22     kindMap = new KindMapping(&context, kindMapInit, "r42a10c14d28i40l41");
23     mod = mlir::ModuleOp::create(mlir::UnknownLoc::get(&context));
24   }
25 
26   void TearDown() { delete kindMap; }
27 
28   mlir::MLIRContext context;
29   KindMapping *kindMap{};
30   std::string kindMapInit =
31       "i10:80,l3:24,a1:8,r54:Double,r62:X86_FP80,r11:PPC_FP128";
32   std::string target = "powerpc64le-unknown-linux-gnu";
33   mlir::ModuleOp mod;
34 };
35 
36 TEST_F(StringAttributesTests, moduleStringAttrTest) {
37   setTargetTriple(mod, target);
38   setKindMapping(mod, *kindMap);
39 
40   auto triple = getTargetTriple(mod);
41   EXPECT_EQ(triple.getArch(), llvm::Triple::ArchType::ppc64le);
42   EXPECT_EQ(triple.getOS(), llvm::Triple::OSType::Linux);
43 
44   auto map = getKindMapping(mod);
45   EXPECT_EQ(map.defaultsToString(), "a10c14d28i40l41r42");
46 
47   auto mapStr = map.mapToString();
48   EXPECT_EQ(mapStr.size(), kindMapInit.size());
49   EXPECT_TRUE(mapStr.find("a1:8") != std::string::npos);
50   EXPECT_TRUE(mapStr.find("l3:24") != std::string::npos);
51   EXPECT_TRUE(mapStr.find("i10:80") != std::string::npos);
52   EXPECT_TRUE(mapStr.find("r11:PPC_FP128") != std::string::npos);
53   EXPECT_TRUE(mapStr.find("r54:Double") != std::string::npos);
54   EXPECT_TRUE(mapStr.find("r62:X86_FP80") != std::string::npos);
55 }
56 
57 // main() from gtest_main
58