1/*******************************************************************************
2* Copyright 2020-2022 Intel Corporation
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*******************************************************************************/
16
17#include <map>
18#include <set>
19#include <utility>
20
21#include "tests/test_isa_common.hpp"
22
23#include "gtest/gtest.h"
24
25#include "oneapi/dnnl/dnnl.hpp"
26#include "src/cpu/x64/cpu_isa_traits.hpp"
27
28namespace dnnl {
29
30TEST(isa_hints_test_t, TestISAHints) {
31 using impl::cpu::x64::cpu_isa_t;
32 auto hints = cpu_isa_hints::prefer_ymm;
33
34 // Use soft version of mayiuse that allows resetting the cpu_isa_hints
35 const bool test_flag = true;
36
37 std::map<cpu_isa_t, bool> compat_before_hint;
38
39 for (auto isa : cpu_isa_list()) {
40 const auto &internal_isa_set = masked_internal_cpu_isa(isa);
41 for (auto internal_isa : internal_isa_set) {
42 compat_before_hint[internal_isa] = mayiuse(internal_isa, test_flag);
43 }
44 }
45
46 std::map<std::pair<cpu_isa_t, cpu_isa_t>, std::pair<bool, bool>>
47 masked_compat_before_hint;
48 for (const auto &isa_pair : hints_masked_internal_cpu_isa(hints)) {
49 masked_compat_before_hint[isa_pair]
50 = {mayiuse(isa_pair.first, test_flag),
51 mayiuse(isa_pair.second, test_flag)};
52 }
53
54 status st = set_cpu_isa_hints(hints);
55 // status::unimplemented if the feature was disabled at compile time
56 if (st == status::unimplemented) return;
57
58 ASSERT_TRUE(st == status::success);
59
60 for (auto isa : cpu_isa_list()) {
61 const auto &internal_isa_set = masked_internal_cpu_isa(isa);
62 for (auto internal_isa : internal_isa_set) {
63 // ISA specific hint will not change the non-hint-complying ISA
64 ASSERT_TRUE(compat_before_hint[internal_isa]
65 == mayiuse(internal_isa, test_flag));
66 }
67 }
68
69 for (const auto &isa_pair : hints_masked_internal_cpu_isa(hints)) {
70 auto compat_pair = masked_compat_before_hint[isa_pair];
71 // isa_pair = {isa_no_hint, isa_hints} is a pair of two ISA that are
72 // only distinguished w.r.t. the CPU ISA hints. Also compat_pair
73 // verifies ISA use before the CPU_ISA hints is applied i.e.
74 //
75 // {compat_pair.frst, compat_pair.second} :=
76 // { mayiuse(isa_no_hints), mayiuse(isa_hints) }
77
78 // CPU_ISA_HINT will not affect the availability of isa_no_hint
79 ASSERT_TRUE(mayiuse(isa_pair.first, test_flag) == compat_pair.first);
80 // Without proper CPU_ISA_HINT isa_hints will not be available
81 ASSERT_FALSE(compat_pair.second);
82
83 // With proper CPU_ISA_HINT isa_hints is available if and only if
84 // isa_no_hints is available
85 ASSERT_TRUE(mayiuse(isa_pair.first, test_flag)
86 == mayiuse(isa_pair.second, test_flag));
87 }
88}
89
90} // namespace dnnl
91