Skip to content

Commit 72a1542

Browse files
jchodorCompute-Runtime-Automation
authored andcommitted
[2/N] Program refactor - gather compiler options
Change-Id: I0a614be73fbd87184be2dfea407582a58c27b0bc
1 parent 49ad839 commit 72a1542

36 files changed

+845
-627
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@ endif()
775775
# Please keep alphabetical order
776776
include_directories(${NEO_BUILD_DIR})
777777
include_directories(${NEO_SOURCE_DIR})
778+
include_directories(${NEO_SOURCE_DIR}/core/compiler_interface/compiler_options${BRANCH_DIR_SUFFIX})
778779
include_directories(${NEO_SOURCE_DIR}/core/memory_manager/definitions${BRANCH_DIR_SUFFIX})
779780
include_directories(${NEO_SOURCE_DIR}/core/memory_properties${BRANCH_DIR_SUFFIX})
780781
include_directories(${NEO_SOURCE_DIR}/core/sku_info/definitions${BRANCH_DIR_SUFFIX})

core/compiler_interface/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ set(NEO_COMPILER_INTERFACE
1414
${CMAKE_CURRENT_SOURCE_DIR}/create_main.cpp
1515
${CMAKE_CURRENT_SOURCE_DIR}/linker.h
1616
${CMAKE_CURRENT_SOURCE_DIR}/linker.cpp
17+
${CMAKE_CURRENT_SOURCE_DIR}/compiler_options/compiler_options_base.h
18+
${CMAKE_CURRENT_SOURCE_DIR}/compiler_options/compiler_options_base.cpp
19+
${CMAKE_CURRENT_SOURCE_DIR}/compiler_options${BRANCH_DIR_SUFFIX}/compiler_options.h
1720
)
1821

1922
set_property(GLOBAL PROPERTY NEO_COMPILER_INTERFACE ${NEO_COMPILER_INTERFACE})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#pragma once
9+
10+
#include "core/compiler_interface/compiler_options/compiler_options_base.h"
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "compiler_options_base.h"
9+
10+
#include <cstring>
11+
12+
namespace NEO {
13+
namespace CompilerOptions {
14+
15+
bool contains(const char *options, ConstStringRef optionToFind) {
16+
auto it = strstr(options, optionToFind.data());
17+
while (it != nullptr) {
18+
const auto delimiter = it[optionToFind.size()];
19+
if ((' ' == delimiter) || ('\0' == delimiter)) {
20+
if ((it == options) || (it[-1] == ' ')) {
21+
return true;
22+
}
23+
}
24+
it = strstr(it + 1, optionToFind.data());
25+
}
26+
return false;
27+
}
28+
29+
bool contains(const std::string &options, ConstStringRef optionToFind) {
30+
return contains(options.c_str(), optionToFind);
31+
}
32+
33+
TokenizedString tokenize(ConstStringRef src) {
34+
TokenizedString ret;
35+
const char *it = src.begin();
36+
while (it < src.end()) {
37+
const char *beg = it;
38+
while ((beg < src.end()) && (*beg == ' ')) {
39+
++beg;
40+
}
41+
const char *end = beg;
42+
while ((end < src.end()) && (*end != ' ')) {
43+
++end;
44+
}
45+
it = end;
46+
if (end != beg) {
47+
ret.push_back(ConstStringRef(beg, end - beg));
48+
}
49+
}
50+
return ret;
51+
};
52+
53+
} // namespace CompilerOptions
54+
} // namespace NEO
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#pragma once
9+
10+
#include "core/utilities/const_stringref.h"
11+
#include "core/utilities/stackvec.h"
12+
13+
namespace NEO {
14+
namespace CompilerOptions {
15+
static constexpr ConstStringRef greaterThan4gbBuffersRequired = "-cl-intel-greater-than-4GB-buffer-required";
16+
static constexpr ConstStringRef hasBufferOffsetArg = "-cl-intel-has-buffer-offset-arg";
17+
static constexpr ConstStringRef kernelDebugEnable = "-cl-kernel-debug-enable";
18+
static constexpr ConstStringRef arch32bit = "-m32";
19+
static constexpr ConstStringRef arch64bit = "-m64";
20+
static constexpr ConstStringRef debugKernelEnable = "-cl-kernel-debug-enable";
21+
static constexpr ConstStringRef optDisable = "-cl-opt-disable";
22+
static constexpr ConstStringRef argInfo = "-cl-kernel-arg-info";
23+
static constexpr ConstStringRef gtpinRera = "-cl-intel-gtpin-rera";
24+
static constexpr ConstStringRef finiteMathOnly = "-cl-finite-math-only";
25+
static constexpr ConstStringRef fastRelaxedMath = "-cl-fast-relaxed-math";
26+
static constexpr ConstStringRef preserveVec3Type = "-fpreserve-vec3-type";
27+
static constexpr ConstStringRef createLibrary = "-create-library";
28+
static constexpr ConstStringRef generateDebugInfo = "-g";
29+
static constexpr ConstStringRef bindlessBuffers = "-cl-intel-use-bindless-buffers";
30+
static constexpr ConstStringRef bindlessImages = "-cl-intel-use-bindless-images";
31+
static constexpr ConstStringRef uniformWorkgroupSize = "-cl-uniform-work-group-size";
32+
33+
constexpr size_t nullterminateSize = 1U;
34+
constexpr size_t spaceSeparatorSize = 1U;
35+
36+
template <size_t Length>
37+
constexpr size_t length(const char (&array)[Length]) {
38+
return Length;
39+
}
40+
41+
constexpr size_t length(ConstStringRef string) {
42+
return string.length();
43+
}
44+
45+
inline size_t length(const std::string &string) {
46+
return string.length();
47+
}
48+
49+
constexpr size_t length(const char *string) {
50+
return constLength(string);
51+
}
52+
53+
constexpr const char *data(ConstStringRef string) {
54+
return string.data();
55+
}
56+
57+
inline const char *data(const std::string &string) {
58+
return string.data();
59+
}
60+
61+
constexpr const char *data(const char *string) {
62+
return string;
63+
}
64+
65+
template <typename T>
66+
constexpr size_t concatenationLength(const T &t) {
67+
return length(t);
68+
}
69+
70+
template <typename T, typename... RestT>
71+
constexpr size_t concatenationLength(const T &arg, const RestT &... rest) {
72+
return length(arg) + spaceSeparatorSize + concatenationLength(rest...);
73+
}
74+
75+
template <typename ContainerT, typename T>
76+
inline void concatenateAppend(ContainerT &out, T &&arg) {
77+
if ((false == out.empty()) && (*out.rbegin() != ' ')) {
78+
out.push_back(' ');
79+
}
80+
out.insert(out.end(), data(arg), data(arg) + length(arg));
81+
}
82+
83+
template <typename ContainerT, typename T, typename... RestT>
84+
inline void concatenateAppend(ContainerT &out, T &&arg, RestT &&... rest) {
85+
concatenateAppend(out, std::forward<T>(arg));
86+
concatenateAppend(out, std::forward<RestT>(rest)...);
87+
}
88+
89+
template <typename T, typename... RestT>
90+
inline std::string concatenate(T &&arg, RestT &&... rest) {
91+
std::string ret;
92+
ret.reserve(nullterminateSize + concatenationLength(arg, rest...));
93+
concatenateAppend(ret, std::forward<T>(arg), std::forward<RestT>(rest)...);
94+
return ret;
95+
}
96+
97+
template <size_t NumOptions>
98+
constexpr size_t concatenationLength(const ConstStringRef (&options)[NumOptions]) {
99+
size_t ret = 0U;
100+
for (auto opt : options) {
101+
ret += spaceSeparatorSize + opt.length();
102+
}
103+
return (ret != 0U) ? ret - nullterminateSize : 0U;
104+
}
105+
106+
template <size_t MaxLength = 256>
107+
class ConstConcatenation {
108+
public:
109+
template <size_t NumOptions>
110+
constexpr ConstConcatenation(const ConstStringRef (&options)[NumOptions]) {
111+
size_t i = 0U;
112+
for (auto opt : options) {
113+
for (size_t j = 0U, e = opt.length(); j < e; ++j, ++i) {
114+
storage[i] = opt[j];
115+
}
116+
storage[i] = ' ';
117+
++i;
118+
}
119+
length = i;
120+
if (i > 0U) {
121+
storage[i - 1] = '\0';
122+
}
123+
}
124+
125+
constexpr operator ConstStringRef() const {
126+
return ConstStringRef(storage, (length > 0U) ? (length - 1) : 0U);
127+
}
128+
129+
constexpr operator const char *() const {
130+
return storage;
131+
}
132+
133+
protected:
134+
char storage[MaxLength + nullterminateSize] = {};
135+
size_t length = 0U;
136+
};
137+
138+
bool contains(const char *options, ConstStringRef optionToFind);
139+
140+
bool contains(const std::string &options, ConstStringRef optionToFind);
141+
142+
using TokenizedString = StackVec<ConstStringRef, 32>;
143+
TokenizedString tokenize(ConstStringRef src);
144+
} // namespace CompilerOptions
145+
} // namespace NEO

core/unit_tests/compiler_interface/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
set(NEO_CORE_COMPILER_INTERFACE_TESTS
88
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
99
${CMAKE_CURRENT_SOURCE_DIR}/compiler_cache_tests.cpp
10+
${CMAKE_CURRENT_SOURCE_DIR}/compiler_options_tests.cpp
1011
${CMAKE_CURRENT_SOURCE_DIR}/compiler_interface_tests.cpp
1112
${CMAKE_CURRENT_SOURCE_DIR}/linker_mock.h
1213
${CMAKE_CURRENT_SOURCE_DIR}/linker_tests.cpp
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2017-2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "test.h"
9+
10+
#include "compiler_options.h"
11+
12+
TEST(CompilerOptions, WhenConcatenationLengthIsCalledThenReturnsSumOfLengthsAndSeperators) {
13+
using namespace NEO::CompilerOptions;
14+
constexpr auto concatenatedLength = concatenationLength(NEO::CompilerOptions::optDisable);
15+
static_assert(optDisable.length() == concatenatedLength, "");
16+
static_assert(optDisable.length() + 1 + gtpinRera.length() == concatenationLength(optDisable, gtpinRera), "");
17+
static_assert(optDisable.length() + 1 + gtpinRera.length() + 1 + finiteMathOnly.length() == concatenationLength(optDisable, gtpinRera, finiteMathOnly), "");
18+
}
19+
20+
TEST(CompilerOptions, WhenConcatenateIsCalledThenUsesSpaceAsSeparator) {
21+
using namespace NEO::CompilerOptions;
22+
auto concatenated = concatenate(NEO::CompilerOptions::optDisable, NEO::CompilerOptions::finiteMathOnly);
23+
auto expected = (std::string(NEO::CompilerOptions::optDisable) + " " + NEO::CompilerOptions::finiteMathOnly.data());
24+
EXPECT_STREQ(expected.c_str(), concatenated.c_str());
25+
26+
constexpr ConstStringRef toConcatenate[] = {"a", "b", "c"};
27+
constexpr ConstConcatenation<concatenationLength(toConcatenate)> constConcatenationSpecificSize(toConcatenate);
28+
constexpr ConstConcatenation<> constConcatenationDefaultSize(toConcatenate);
29+
EXPECT_TRUE(ConstStringRef("a b c") == constConcatenationSpecificSize);
30+
EXPECT_TRUE(ConstStringRef("a b c") == constConcatenationDefaultSize);
31+
}
32+
33+
TEST(CompilerOptions, WhenConcatenateAppendIsCalledThenAddsSpaceAsSeparatorOnlyIfMissing) {
34+
using namespace NEO::CompilerOptions;
35+
std::string concatenated = NEO::CompilerOptions::optDisable.data();
36+
concatenateAppend(concatenated, NEO::CompilerOptions::finiteMathOnly);
37+
auto expected = (std::string(NEO::CompilerOptions::optDisable) + " " + NEO::CompilerOptions::finiteMathOnly.data());
38+
EXPECT_STREQ(expected.c_str(), concatenated.c_str());
39+
concatenated += " ";
40+
concatenateAppend(concatenated, NEO::CompilerOptions::fastRelaxedMath);
41+
expected += " ";
42+
expected += NEO::CompilerOptions::fastRelaxedMath;
43+
EXPECT_STREQ(expected.c_str(), concatenated.c_str());
44+
}
45+
46+
TEST(CompilerOptions, WhenCheckingForPresenceOfOptionThenRejectsSubstrings) {
47+
EXPECT_FALSE(NEO::CompilerOptions::contains("aaa", "a"));
48+
EXPECT_FALSE(NEO::CompilerOptions::contains("aaa", "aa"));
49+
EXPECT_TRUE(NEO::CompilerOptions::contains("aaa", "aaa"));
50+
EXPECT_FALSE(NEO::CompilerOptions::contains("aaa", "aaaa"));
51+
EXPECT_TRUE(NEO::CompilerOptions::contains("aaaa aaa", "aaaa"));
52+
EXPECT_TRUE(NEO::CompilerOptions::contains("aa aaaa", "aaaa"));
53+
}
54+
55+
TEST(CompilerOptions, WhenTokenizingThenSpaceIsUsedAsSeparator) {
56+
auto tokenizedEmpty = NEO::CompilerOptions::tokenize("");
57+
EXPECT_TRUE(tokenizedEmpty.empty());
58+
59+
auto tokenizedOne = NEO::CompilerOptions::tokenize("abc");
60+
ASSERT_EQ(1U, tokenizedOne.size());
61+
EXPECT_EQ("abc", tokenizedOne[0]);
62+
63+
auto tokenizedOneSkipSpaces = NEO::CompilerOptions::tokenize(" abc ");
64+
ASSERT_EQ(1U, tokenizedOneSkipSpaces.size());
65+
EXPECT_EQ("abc", tokenizedOneSkipSpaces[0]);
66+
67+
auto tokenizedMultiple = NEO::CompilerOptions::tokenize(" -optA -optB c ");
68+
ASSERT_EQ(3U, tokenizedMultiple.size());
69+
EXPECT_EQ("-optA", tokenizedMultiple[0]);
70+
EXPECT_EQ("-optB", tokenizedMultiple[1]);
71+
EXPECT_EQ("c", tokenizedMultiple[2]);
72+
}

core/unit_tests/utilities/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(NEO_CORE_UTILITIES_TESTS
88
${CMAKE_CURRENT_SOURCE_DIR}/base_object_utils.h
99
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
1010
${CMAKE_CURRENT_SOURCE_DIR}/clflush_tests.cpp
11+
${CMAKE_CURRENT_SOURCE_DIR}/const_stringref_tests.cpp
1112
${CMAKE_CURRENT_SOURCE_DIR}/containers_tests.cpp
1213
${CMAKE_CURRENT_SOURCE_DIR}/containers_tests_helpers.h
1314
${CMAKE_CURRENT_SOURCE_DIR}/cpuinfo_tests.cpp
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (C) 2017-2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "core/utilities/const_stringref.h"
9+
10+
#include "gtest/gtest.h"
11+
12+
TEST(ConstStringRef, WhenCreatingFromConstantArrayThenIsConstexpr) {
13+
static constexpr ConstStringRef str0("some_text");
14+
static_assert(9 == str0.length(), "");
15+
static_assert(9 == str0.size(), "");
16+
static_assert(false == str0.empty(), "");
17+
static_assert('s' == str0[0], "");
18+
static_assert('o' == str0[1], "");
19+
static_assert('m' == str0[2], "");
20+
static_assert('e' == str0[3], "");
21+
static_assert('_' == str0[4], "");
22+
static_assert('t' == str0[5], "");
23+
static_assert('e' == str0[6], "");
24+
static_assert('x' == str0[7], "");
25+
static_assert('t' == str0[8], "");
26+
27+
static constexpr ConstStringRef str1("second", 3);
28+
static_assert(3 == str1.length(), "");
29+
static_assert(3 == str1.size(), "");
30+
static_assert(false == str1.empty(), "");
31+
static_assert('s' == str1[0], "");
32+
static_assert('e' == str1[1], "");
33+
static_assert('c' == str1[2], "");
34+
35+
static_assert('s' == static_cast<const char *>(str1)[0], "");
36+
static_assert('s' == *str1.begin(), "");
37+
static_assert(3 == str1.end() - str1.begin(), "");
38+
static_assert(str1.begin() == str1.data(), "");
39+
40+
static constexpr ConstStringRef strEmpty("aaa", 0);
41+
static_assert(0U == strEmpty.length(), "");
42+
static_assert(0U == strEmpty.size(), "");
43+
static_assert(strEmpty.empty(), "");
44+
45+
static_assert(9 == str0.length(), "");
46+
47+
static_assert(str0 == str0, "");
48+
static_assert(str1 != str0, "");
49+
50+
static constexpr ConstStringRef strAbc("abc");
51+
static constexpr ConstStringRef strAbd("abd");
52+
static constexpr ConstStringRef strAbc2("abcdef", 3);
53+
static_assert(strAbc != strAbd, "");
54+
static_assert(strAbc.length() == strAbc2.length(), "");
55+
static_assert(strAbc == strAbc2, "");
56+
}
57+
58+
TEST(ConstStringRef, WhenComparingAgainstContainersThenUsesLexicographicOrdering) {
59+
static constexpr ConstStringRef constStrText("Text");
60+
std::string strText("Text");
61+
std::string strToxt("Toxt");
62+
EXPECT_TRUE(strText == constStrText);
63+
EXPECT_TRUE(constStrText == strText);
64+
EXPECT_FALSE(strToxt == constStrText);
65+
EXPECT_FALSE(constStrText == strToxt);
66+
67+
EXPECT_FALSE(strText != constStrText);
68+
EXPECT_FALSE(constStrText != strText);
69+
EXPECT_TRUE(strToxt != constStrText);
70+
EXPECT_TRUE(constStrText != strToxt);
71+
72+
std::string strTex("Tex");
73+
EXPECT_TRUE(strTex != constStrText);
74+
EXPECT_TRUE(constStrText != strTex);
75+
}

0 commit comments

Comments
 (0)