Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e1322e

Browse files
authored
Merge pull request iree-org#8436 from google/benvanik-processor-info
Adding an executable environment and plumbing through processor information.
2 parents 3bcc2d3 + 9f3b9fe commit 7e1322e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+822
-268
lines changed

‎experimental/rocm/native_executable.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ static iree_hal_rocm_native_executable_t* iree_hal_rocm_native_executable_cast(
4646

4747
iree_status_t iree_hal_rocm_native_executable_create(
4848
iree_hal_rocm_context_wrapper_t* context,
49-
const iree_hal_executable_spec_t* executable_spec,
49+
const iree_hal_executable_params_t* executable_params,
5050
iree_hal_executable_t** out_executable) {
5151
IREE_ASSERT_ARGUMENT(context);
52-
IREE_ASSERT_ARGUMENT(executable_spec);
52+
IREE_ASSERT_ARGUMENT(executable_params);
5353
IREE_ASSERT_ARGUMENT(out_executable);
5454
*out_executable = NULL;
5555
IREE_TRACE_ZONE_BEGIN(z0);
@@ -58,7 +58,7 @@ iree_status_t iree_hal_rocm_native_executable_create(
5858

5959
// TODO: Verify the flat buffer.
6060
iree_ROCMExecutableDef_table_t executable_def =
61-
iree_ROCMExecutableDef_as_root(executable_spec->executable_data.data);
61+
iree_ROCMExecutableDef_as_root(executable_params->executable_data.data);
6262

6363
// Create the kernel module.
6464
flatbuffers_string_t hsaco_image =
@@ -96,8 +96,9 @@ iree_status_t iree_hal_rocm_native_executable_create(
9696
executable->entry_functions[i].block_size_y = block_sizes_vec[i].y;
9797
executable->entry_functions[i].block_size_z = block_sizes_vec[i].z;
9898
executable->executable_layouts[i] =
99-
executable_spec->executable_layouts[i];
100-
iree_hal_executable_layout_retain(executable_spec->executable_layouts[i]);
99+
executable_params->executable_layouts[i];
100+
iree_hal_executable_layout_retain(
101+
executable_params->executable_layouts[i]);
101102
}
102103
}
103104

‎experimental/rocm/native_executable.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern "C" {
2222
// kernels that can be extracted along with the associated block size.
2323
iree_status_t iree_hal_rocm_native_executable_create(
2424
iree_hal_rocm_context_wrapper_t* context,
25-
const iree_hal_executable_spec_t* executable_spec,
25+
const iree_hal_executable_params_t* executable_params,
2626
iree_hal_executable_t** out_executable);
2727

2828
hipFunction_t iree_hal_rocm_native_executable_for_entry_point(

‎experimental/rocm/nop_executable_cache.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ static bool iree_hal_rocm_nop_executable_cache_can_prepare_format(
7272

7373
static iree_status_t iree_hal_rocm_nop_executable_cache_prepare_executable(
7474
iree_hal_executable_cache_t* base_executable_cache,
75-
const iree_hal_executable_spec_t* executable_spec,
75+
const iree_hal_executable_params_t* executable_params,
7676
iree_hal_executable_t** out_executable) {
7777
iree_hal_rocm_nop_executable_cache_t* executable_cache =
7878
iree_hal_rocm_nop_executable_cache_cast(base_executable_cache);
7979
return iree_hal_rocm_native_executable_create(
80-
executable_cache->context, executable_spec, out_executable);
80+
executable_cache->context, executable_params, out_executable);
8181
}
8282

8383
static const iree_hal_executable_cache_vtable_t

‎experimental/web/sample_static/device_multithreaded.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ iree_status_t create_device_with_static_loader(iree_allocator_t host_allocator,
1616
iree_hal_task_device_params_t params;
1717
iree_hal_task_device_params_initialize(&params);
1818

19-
// Load the statically embedded library.
20-
const iree_hal_executable_library_header_t** static_library =
21-
mnist_linked_llvm_library_query(
22-
IREE_HAL_EXECUTABLE_LIBRARY_LATEST_VERSION,
23-
/*reserved=*/NULL);
24-
const iree_hal_executable_library_header_t** libraries[1] = {static_library};
25-
19+
// Register the statically linked executable library.
20+
const iree_hal_executable_library_query_fn_t* libraries[] = {
21+
mnist_linked_llvm_library_query,
22+
};
2623
iree_hal_executable_loader_t* library_loader = NULL;
2724
iree_status_t status = iree_hal_static_library_loader_create(
2825
IREE_ARRAYSIZE(libraries), libraries,

‎experimental/web/sample_static/device_sync.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ iree_status_t create_device_with_static_loader(iree_allocator_t host_allocator,
1313
iree_hal_sync_device_params_t params;
1414
iree_hal_sync_device_params_initialize(&params);
1515

16-
// Load the statically embedded library.
17-
const iree_hal_executable_library_header_t** static_library =
18-
mnist_linked_llvm_library_query(
19-
IREE_HAL_EXECUTABLE_LIBRARY_LATEST_VERSION,
20-
/*reserved=*/NULL);
21-
const iree_hal_executable_library_header_t** libraries[1] = {static_library};
22-
16+
// Register the statically linked executable library.
17+
const iree_hal_executable_library_query_fn_t* libraries[] = {
18+
mnist_linked_llvm_library_query,
19+
};
2320
iree_hal_executable_loader_t* library_loader = NULL;
2421
iree_status_t status = iree_hal_static_library_loader_create(
2522
IREE_ARRAYSIZE(libraries), libraries,

‎iree/base/internal/BUILD‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ cc_test(
102102
],
103103
)
104104

105+
cc_library(
106+
name = "cpu",
107+
srcs = ["cpu.c"],
108+
hdrs = ["cpu.h"],
109+
deps = [
110+
"//iree/base",
111+
"//iree/base:core_headers",
112+
],
113+
)
114+
105115
cc_library(
106116
name = "dynamic_library",
107117
srcs = [
@@ -277,6 +287,7 @@ cc_library(
277287
],
278288
deps = [
279289
":internal",
290+
"//build_tools:default_linkopts",
280291
"//iree/base",
281292
"//iree/base:core_headers",
282293
"//iree/base:tracing",

‎iree/base/internal/CMakeLists.txt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ iree_cc_test(
9292
iree::testing::gtest_main
9393
)
9494

95+
iree_cc_library(
96+
NAME
97+
cpu
98+
HDRS
99+
"cpu.h"
100+
SRCS
101+
"cpu.c"
102+
DEPS
103+
iree::base
104+
iree::base::core_headers
105+
PUBLIC
106+
)
107+
95108
iree_cc_library(
96109
NAME
97110
dynamic_library

‎iree/base/internal/cpu.c‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2022 The IREE Authors
2+
//
3+
// 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+
#include "iree/base/internal/cpu.h"
8+
9+
#include "iree/base/target_platform.h"
10+
11+
//===----------------------------------------------------------------------===//
12+
// iree_cpu_*
13+
//===----------------------------------------------------------------------===//
14+
15+
#if defined(IREE_PLATFORM_ANDROID) || defined(IREE_PLATFORM_EMSCRIPTEN) || \
16+
defined(IREE_PLATFORM_LINUX)
17+
18+
#include <sched.h>
19+
20+
extern __attribute__((weak)) int sched_getcpu(void) {
21+
// TODO(benvanik): emulate with syscall/vdso/etc.
22+
errno = ENOSYS;
23+
return -1;
24+
}
25+
26+
iree_cpu_processor_id_t iree_cpu_query_processor_id(void) {
27+
// This path is relatively portable and should work on linux/bsd/etc-likes.
28+
// We may want to use getcpu when available so that we can get the group ID.
29+
// https://man7.org/linux/man-pages/man3/sched_getcpu.3.html
30+
//
31+
// libc implementations can use vDSO and other fun stuff to make this really
32+
// cheap: http://git.musl-libc.org/cgit/musl/tree/src/sched/sched_getcpu.c
33+
int id = sched_getcpu();
34+
return id != -1 ? id : 0;
35+
}
36+
37+
#elif defined(IREE_PLATFORM_WINDOWS)
38+
39+
iree_cpu_processor_id_t iree_cpu_query_processor_id(void) {
40+
PROCESSOR_NUMBER pn;
41+
GetCurrentProcessorNumberEx(&pn);
42+
return 64 * pn.Group + pn.Number;
43+
}
44+
45+
#else
46+
47+
// No implementation.
48+
// We could allow an iree/base/config.h override to externalize this.
49+
iree_cpu_processor_id_t iree_cpu_query_processor_id(void) { return 0; }
50+
51+
#endif // IREE_PLATFORM_*
52+
53+
void iree_cpu_requery_processor_id(iree_cpu_processor_tag_t* IREE_RESTRICT tag,
54+
iree_cpu_processor_id_t* IREE_RESTRICT
55+
processor_id) {
56+
IREE_ASSERT_ARGUMENT(tag);
57+
IREE_ASSERT_ARGUMENT(processor_id);
58+
59+
// TODO(benvanik): set a frequency for this and use a coarse timer
60+
// (CLOCK_MONOTONIC_COARSE) to do a ~4-10Hz refresh. We can store the last
61+
// query time and the last processor ID in the tag and only perform the query
62+
// if it has changed.
63+
64+
*processor_id = iree_cpu_query_processor_id();
65+
}

‎iree/base/internal/cpu.h‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2022 The IREE Authors
2+
//
3+
// 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+
#ifndef IREE_BASE_INTERNAL_CPU_H_
8+
#define IREE_BASE_INTERNAL_CPU_H_
9+
10+
#include <stddef.h>
11+
12+
#include "iree/base/api.h"
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif // __cplusplus
17+
18+
//===----------------------------------------------------------------------===//
19+
// iree_cpu_*
20+
//===----------------------------------------------------------------------===//
21+
22+
typedef uint32_t iree_cpu_processor_id_t;
23+
typedef uint32_t iree_cpu_processor_tag_t;
24+
25+
// Returns the ID of the logical processor executing this code.
26+
iree_cpu_processor_id_t iree_cpu_query_processor_id(void);
27+
28+
// Returns the ID of the logical processor executing this code, using |tag| to
29+
// memoize the query in cases where it does not change frequently.
30+
// |tag| must be initialized to 0 on first call and may be reset to 0 by the
31+
// caller at any time to invalidate the cached result.
32+
void iree_cpu_requery_processor_id(iree_cpu_processor_tag_t* IREE_RESTRICT tag,
33+
iree_cpu_processor_id_t* IREE_RESTRICT
34+
processor_id);
35+
36+
#ifdef __cplusplus
37+
} // extern "C"
38+
#endif // __cplusplus
39+
40+
#endif // IREE_BASE_INTERNAL_ARENA_H_

0 commit comments

Comments
 (0)