1
+ // ===-- TargetDescriptionAnalysisTest.cpp -----------------------*- C++ -*-===//
2
+ //
3
+ // This file is licensed 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
+ #include " gc/Analysis/TargetDescriptionAnalysis.h"
9
+ #include " gc/ExecutionEngine/Driver/Driver.h"
10
+ #include " mlir/AsmParser/AsmParser.h"
11
+ #include " mlir/ExecutionEngine/MemRefUtils.h"
12
+ #include " mlir/IR/AsmState.h"
13
+ #include " mlir/IR/BuiltinOps.h"
14
+ #include " mlir/IR/MLIRContext.h"
15
+ #include " mlir/Parser/Parser.h"
16
+ #include " mlir/Pass/PassManager.h"
17
+ #include " llvm/Support/ErrorOr.h"
18
+ #include " llvm/Support/MemoryBuffer.h"
19
+ #include " llvm/Support/SourceMgr.h"
20
+ #include " llvm/Support/raw_ostream.h"
21
+ #include " gtest/gtest.h"
22
+ #include < memory>
23
+
24
+ using namespace mlir ;
25
+
26
+ static const char code1[] = R"mlir(
27
+ module attributes {
28
+ dlti.target_system_spec = #dlti.target_system_spec<
29
+ "CPU": #dlti.target_device_spec<
30
+ #dlti.dl_entry<"L1_cache_size_in_bytes", 49152 : ui32>,
31
+ #dlti.dl_entry<"L2_cache_size_in_bytes", 2097152 : ui64>,
32
+ #dlti.dl_entry<"L3_cache_size_in_bytes", "110100480">,
33
+ #dlti.dl_entry<"num_threads", 56 : i32>,
34
+ #dlti.dl_entry<"max_vector_width", 512 : i64>>
35
+ >} {}
36
+ )mlir" ;
37
+
38
+ TEST (TargetDescriptionAnalysis, CPUNormal) {
39
+ MLIRContext ctx{gc::initCompilerAndGetDialects ()};
40
+ std::unique_ptr<llvm::MemoryBuffer> ir_buffer =
41
+ llvm::MemoryBuffer::getMemBuffer (code1);
42
+ // Parse the input mlir.
43
+ llvm::SourceMgr sourceMgr;
44
+ sourceMgr.AddNewSourceBuffer (std::move (ir_buffer), llvm::SMLoc ());
45
+ mlir::OwningOpRef<mlir::ModuleOp> module =
46
+ mlir::parseSourceFile<mlir::ModuleOp>(sourceMgr, &ctx);
47
+ ASSERT_TRUE (module);
48
+ auto CPUTagetDesc = gc::CPUTargetDescriptionAnalysis (module.get ());
49
+ ASSERT_EQ (CPUTagetDesc.getNumThreads (), 56 );
50
+ ASSERT_EQ (CPUTagetDesc.getCacheSize (1 ), 49152 );
51
+ ASSERT_EQ (CPUTagetDesc.getCacheSize (2 ), 2097152 );
52
+ ASSERT_EQ (CPUTagetDesc.getCacheSize (3 ), 110100480 );
53
+ ASSERT_EQ (CPUTagetDesc.getMaxVectorWidth (), 512 );
54
+ }
55
+
56
+ static const char code2[] = R"mlir(
57
+ module attributes {
58
+ dlti.target_system_spec = #dlti.target_system_spec<
59
+ "CPU": #dlti.target_device_spec<
60
+ #dlti.dl_entry<"L1_cache_size_in_bytes", 49152 : ui32>,
61
+ #dlti.dl_entry<"L2_cache_size_in_bytes", 2097152 : ui32>>
62
+ >} {}
63
+ )mlir" ;
64
+
65
+ TEST (TargetDescriptionAnalysis, CPUMissingValue) {
66
+ MLIRContext ctx{gc::initCompilerAndGetDialects ()};
67
+ std::unique_ptr<llvm::MemoryBuffer> ir_buffer =
68
+ llvm::MemoryBuffer::getMemBuffer (code2);
69
+ // Parse the input mlir.
70
+ llvm::SourceMgr sourceMgr;
71
+ sourceMgr.AddNewSourceBuffer (std::move (ir_buffer), llvm::SMLoc ());
72
+ mlir::OwningOpRef<mlir::ModuleOp> module =
73
+ mlir::parseSourceFile<mlir::ModuleOp>(sourceMgr, &ctx);
74
+ ASSERT_TRUE (module);
75
+ auto CPUTagetDesc = gc::CPUTargetDescriptionAnalysis (module.get ());
76
+ ASSERT_EQ (CPUTagetDesc.getNumThreads (), 1 );
77
+ ASSERT_EQ (CPUTagetDesc.getCacheSize (1 ), 49152 );
78
+ ASSERT_EQ (CPUTagetDesc.getCacheSize (2 ), 2097152 );
79
+ ASSERT_EQ (CPUTagetDesc.getCacheSize (3 ), 1048576 );
80
+ ASSERT_EQ (CPUTagetDesc.getMaxVectorWidth (), 512 );
81
+ }
0 commit comments