Skip to content

Commit

Permalink
Use snippets for runtime API samples, add line numbers to code blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottTodd committed Nov 27, 2023
1 parent 21d94aa commit f035053
Showing 1 changed file with 17 additions and 52 deletions.
69 changes: 17 additions & 52 deletions docs/website/docs/reference/bindings/c-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,15 @@ options, passes, or pipelines. For documentation on compiler plugins, see

To build a custom tool using the compiler API:

```cmake title="CMakeLists.txt"
<!-- TODO(scotttodd): snippet that links to a working in-tree sample instead -->

```cmake title="CMakeLists.txt" linenums="1"
set(_IREE_COMPILER_API "${_IREE_COMPILER_ROOT}/bindings/c/iree/compiler")
target_include_directories(${_NAME} SYSTEM PRIVATE ${_IREE_COMPILER_API})
target_link_libraries(${_NAME} iree_compiler_bindings_c_loader)
```

```c title="iree_compiler_demo.c"
```c title="iree_compiler_demo.c" linenums="1"
#include <iree/compiler/embedding_api.h>
#include <iree/compiler/loader.h>

Expand Down Expand Up @@ -458,60 +460,23 @@ hardware devices like CPUs, GPUs and other accelerators.

!!! info ""

This snippet shows the general layout of the API. For working examples, see
the [samples below](#samples_1).
For other examples, see the [samples below](#samples_1).

```cmake title="CMakeLists.txt"
target_include_directories(${_NAME} SYSTEM PRIVATE ${_IREE_RUNTIME_ROOT})
target_link_libraries(${_NAME} iree_runtime_runtime)
```
=== "hello_world_terse.c"

```c title="iree_runtime_demo.c"
#include <iree/runtime/api.h>
Source file: [`runtime/src/iree/runtime/demo/hello_world_terse.c`](https://github.com/openxla/iree/tree/main/runtime/src/iree/runtime/demo/hello_world_terse.c)

int main(int argc, char** argv) {
// Setup the shared runtime instance.
iree_runtime_instance_options_t instance_options;
iree_runtime_instance_options_initialize(&instance_options);
iree_runtime_instance_options_use_all_available_drivers(&instance_options);
iree_runtime_instance_t* instance = NULL;
iree_runtime_instance_create(
&instance_options, iree_allocator_system(), &instance);

// Create the HAL device used to run the workloads.
iree_hal_device_t* device = NULL;
iree_runtime_instance_try_create_default_device(
instance, iree_make_cstring_view("local-task"), &device);

// Create a session to hold the module state.
iree_runtime_session_options_t session_options;
iree_runtime_session_options_initialize(&session_options);
iree_runtime_session_t* session = NULL;
iree_runtime_session_create_with_device(
instance, &session_options, device,
iree_runtime_instance_host_allocator(instance), &session);

// Load the compiled user module from a file.
iree_runtime_session_append_bytecode_module_from_file(
session, "program.vmfb");

// Build and issue the call.
iree_runtime_call_t call;
iree_runtime_call_initialize_by_name(
session, iree_make_cstring_view("module.entry_function_name"), &call);
// iree_runtime_call_inputs_push_back_buffer_view(...);
iree_runtime_call_invoke(&call, /*flags=*/0);

// Retrieve the function outputs and clean up the call.
// iree_runtime_call_outputs_pop_front_buffer_view(...);
iree_runtime_call_deinitialize(&call);
```c++ title="runtime/src/iree/runtime/demo/hello_world_terse.c" linenums="1"
--8<-- "runtime/src/iree/runtime/demo/hello_world_terse.c:7"
```

// Cleanup state.
iree_runtime_session_release(session);
iree_hal_device_release(device);
iree_runtime_instance_release(instance);
}
```
=== "hello_world_explained.c"

Source file: [`runtime/src/iree/runtime/demo/hello_world_explained.c`](https://github.com/openxla/iree/tree/main/runtime/src/iree/runtime/demo/hello_world_explained.c)

```c++ title="runtime/src/iree/runtime/demo/hello_world_explained.c" linenums="1"
--8<-- "runtime/src/iree/runtime/demo/hello_world_explained.c:7"
```

#### Samples

Expand Down

0 comments on commit f035053

Please sign in to comment.