Repository navigation
Expand file tree
/
Copy pathtest_static_plugins.cpp
More file actions
371 lines (333 loc) · 14.5 KB
/
Copy pathtest_static_plugins.cpp
File metadata and controls
371 lines (333 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved.
* Licensed under the MIT License.
*/
// Static compiler-plugin unit test.
//
// Runs the static registrar (dispatchPluginRegistrationsOnce,
// StaticPlugins.cpp) and checks the recorded registry state. CMake sets
// HIP_EP_EXPECT_SAMPLE:
// * 1 (sample selected): the registry records the sample's slot request,
// library + search path, bitcode buffer (empty => degraded no-clang build,
// skipped), and dialect registration (loaded here via loadAllDialects).
// * 0 (no plugins): dispatch is a clean no-op; the registry stays empty.
// Either way dispatch must be safe + idempotent. Plain main() (no GTest).
#include "hip/Compiler/PluginRegistry.h"
#include "hip/Dialect/IR/HipDialect.h"
#include "hip/Dialect/Transforms/Passes.h"
#include "hip/Dialect/Transforms/Pipelines.h"
#include "hip/InitAllPasses.h"
#include "morphizen-foundation/file_io.hpp"
#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/Parser/Parser.h"
#include "mlir/Pass/PassManager.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#ifndef HIP_EP_EXPECT_SAMPLE
#define HIP_EP_EXPECT_SAMPLE 0
#endif
extern "C" void
hipEpRegisterPlugin_sample(hip::compiler::HipEpPluginRegistry ®istry);
namespace {
int g_failures = 0;
void check(bool cond, llvm::StringRef what) {
if (cond) {
llvm::outs() << "[ OK ] " << what << "\n";
} else {
llvm::errs() << "[FAIL] " << what << "\n";
++g_failures;
}
}
template <typename Range>
bool contains(const Range &range, llvm::StringRef needle) {
for (const auto &e : range)
if (llvm::StringRef(e) == needle)
return true;
return false;
}
class CapturingFileSystem : public morphizen::FileSystem {
public:
class Writer : public morphizen::FileWriter {
public:
explicit Writer(std::vector<char> &data) : data(data) {}
std::size_t fwrite(const void *source, std::size_t size) const override {
const char *bytes = static_cast<const char *>(source);
data.insert(data.end(), bytes, bytes + size);
return size;
}
private:
std::vector<char> &data;
};
morphizen::FileReader *create_reader(const char *) override {
return nullptr;
}
morphizen::FileWriter *create_writer(const char *path) override {
return new Writer(files[path]);
}
void destroy_reader(morphizen::FileReader *reader) override { delete reader; }
void destroy_writer(morphizen::FileWriter *writer) override { delete writer; }
std::map<std::string, std::vector<char>> files;
};
mlir::OwningOpRef<mlir::ModuleOp>
runOnnxToHipPipeline(mlir::MLIRContext &context, llvm::StringRef text,
CapturingFileSystem &fs, bool &ok) {
auto module = mlir::parseSourceString<mlir::ModuleOp>(text, &context);
if (!module) {
ok = false;
return module;
}
mlir::PassManager manager(&context);
mlir::hip::OnnxToHipPipelineOptions options;
options.externalizeMinNumElements = 1;
mlir::hip::buildOnnxToHipPipeline(manager, options, &fs);
ok = mlir::succeeded(manager.run(*module));
return module;
}
mlir::OwningOpRef<mlir::ModuleOp>
runMemoryExternalizer(mlir::MLIRContext &context, llvm::StringRef text,
CapturingFileSystem &fs, int64_t minElements,
bool skipData, bool &ok) {
auto module = mlir::parseSourceString<mlir::ModuleOp>(text, &context);
if (!module) {
ok = false;
return module;
}
mlir::PassManager manager(&context);
manager.addPass(
mlir::hip::createExternalizeConstantsPass(&fs, minElements, skipData));
ok = mlir::succeeded(manager.run(*module));
return module;
}
std::string memorySourceModule(uintptr_t address) {
return "module {\n"
" func.func @memory_source() -> tensor<4xui8> {\n"
" %0 = hip.constant {memory_address = " +
std::to_string(address) +
" : i64, size = 4 : i64} : tensor<4xui8>\n"
" return %0 : tensor<4xui8>\n"
" }\n"
"}\n";
}
} // namespace
int main() {
using namespace hip::compiler;
// Dispatch must be safe to call and idempotent (call twice).
dispatchPluginRegistrationsOnce();
dispatchPluginRegistrationsOnce();
check(true, "dispatchPluginRegistrationsOnce() ran without crashing");
auto slotPasses = pluginPassesForSlot(PipelineSlot::AfterConvertOnnxToHip);
auto lateSlotPasses = pluginPassesForSlot(PipelineSlot::BeforeBufferization);
auto libs = pluginLibraries();
auto libPaths = pluginLibraryPaths();
auto bitcode = pluginBitcodeBuffers();
auto dialectRegs = pluginDialectRegistrations();
#if HIP_EP_EXPECT_SAMPLE
llvm::outs() << "Mode: sample plugin EXPECTED (statically linked)\n";
check(contains(slotPasses, "func.func(hip-ep-sample-print-functions)"),
"AfterConvertOnnxToHip slot records the sample pass request");
check(contains(lateSlotPasses, "func.func(hip-ep-sample-emit-late-constant)"),
"BeforeBufferization slot records the late-carrier fixture");
check(contains(libs, "hip_ep_sample_lib"),
"pluginLibraries() records 'hip_ep_sample_lib'");
check(!libPaths.empty(), "pluginLibraryPaths() records a search path");
// Bitcode is present only when the build had clang to compile it; an empty
// set is the documented degraded-build case, not a failure.
if (bitcode.empty()) {
llvm::outs() << "[SKIP] no plugin bitcode (degraded build without clang)\n";
} else {
check(bitcode.size() == 1, "exactly one plugin bitcode buffer recorded");
const auto &buf = bitcode.front();
const auto *bytes = static_cast<const unsigned char *>(buf.data);
bool magicOk = buf.sizeBytes >= 4 && bytes[0] == 'B' && bytes[1] == 'C' &&
bytes[2] == 0xC0 && bytes[3] == 0xDE;
check(magicOk, "plugin bitcode carries the LLVM bitcode magic");
}
// Dialect-registration path. The sample plugin contributes a minimal vendor
// dialect (hip_ep_sample) with a ConvertToLLVMPatternInterface. This is the
// only in-tree coverage of loadAllDialects()'s plugin loop and the
// convert-hip-to-llvm hasPromisedInterface guard, so exercise both here.
check(!dialectRegs.empty(),
"pluginDialectRegistrations() records the sample dialect callback");
mlir::MLIRContext context;
hip::compiler::loadAllDialects(context);
mlir::Dialect *sampleDialect = context.getLoadedDialect("hip_ep_sample");
check(sampleDialect != nullptr,
"loadAllDialects() loaded the plugin's hip_ep_sample dialect");
if (sampleDialect) {
// Mirror the convert-hip-to-llvm guard: a genuinely-registered interface
// (attached via DialectExtension, not just promised) has
// hasPromisedInterface() false AND dyn_casts, so its patterns are used.
bool onlyPromised = sampleDialect->hasPromisedInterface(
sampleDialect->getTypeID(),
mlir::ConvertToLLVMPatternInterface::getInterfaceID());
check(!onlyPromised,
"hip_ep_sample's ConvertToLLVM interface is registered, not just "
"promised (hasPromisedInterface == false)");
check(mlir::dyn_cast<mlir::ConvertToLLVMPatternInterface>(sampleDialect) !=
nullptr,
"hip_ep_sample implements ConvertToLLVMPatternInterface "
"(convert-hip-to-llvm guard admits it)");
}
#else
llvm::outs() << "Mode: NO plugins selected (dispatch must be a no-op)\n";
check(slotPasses.empty(), "no plugin slot requests recorded");
check(lateSlotPasses.empty(), "no late plugin slot requests recorded");
check(libs.empty(), "no plugin libraries recorded");
check(libPaths.empty(), "no plugin library paths recorded");
check(bitcode.empty(), "no plugin bitcode recorded");
check(dialectRegs.empty(), "no plugin dialect registrations recorded");
// The authoritative default build deliberately selects no production
// plugins, but still links the real sample archive into this focused test.
// Register it explicitly and exercise the actual pipeline slots so carrier
// production/consumption and the late-carrier diagnostic run in default CI.
hipEpRegisterPlugin_sample(getProcessPluginRegistry());
check(contains(pluginPassesForSlot(PipelineSlot::AfterConvertOnnxToHip),
"func.func(hip-ep-sample-print-functions)"),
"focused sample registration records AfterConvertOnnxToHip");
check(contains(pluginPassesForSlot(PipelineSlot::BeforeBufferization),
"func.func(hip-ep-sample-emit-late-constant)"),
"focused sample registration records BeforeBufferization");
mlir::MLIRContext context;
context.allowUnregisteredDialects();
hip::compiler::loadAllDialects(context);
CapturingFileSystem carrierFs;
bool carrierOk = false;
auto carrierModule = runOnnxToHipPipeline(context,
R"mlir(
module {
func.func @main_graph(
%input: tensor<2xf32> {onnx.name = "input"})
-> tensor<2xf32> attributes {hip_ep_sample.emit_constant} {
"onnx.Return"(%input) : (tensor<2xf32>) -> ()
}
}
)mlir",
carrierFs, carrierOk);
check(carrierOk && carrierModule,
"AfterConvert sample carrier pipeline succeeds");
if (carrierOk && carrierModule) {
auto sizes =
(*carrierModule)
->getAttrOfType<mlir::DenseI64ArrayAttr>("hipdnn.constant_sizes");
auto offsets =
(*carrierModule)
->getAttrOfType<mlir::DenseI64ArrayAttr>("hipdnn.constant_offsets");
size_t carriers = 0;
carrierModule->walk([&](mlir::hip::ConstantOp) { ++carriers; });
const std::array<float, 2> expectedWeight = {101.0f, 102.0f};
std::vector<char> expectedCarrierBytes(sizeof(expectedWeight));
std::memcpy(expectedCarrierBytes.data(), expectedWeight.data(),
sizeof(expectedWeight));
check(sizes && sizes.asArrayRef().size() == 1 &&
sizes.asArrayRef()[0] ==
static_cast<int64_t>(sizeof(expectedWeight)) &&
offsets && offsets.asArrayRef().size() == 1 &&
offsets.asArrayRef()[0] == 0 &&
carrierFs.files["model.constants.bin"] == expectedCarrierBytes &&
carriers == 0,
"AfterConvert sample carrier is externalized with exact artifact");
}
bool sawLateCarrierDiagnostic = false;
mlir::ScopedDiagnosticHandler diagnosticHandler(
&context, [&](mlir::Diagnostic &diagnostic) {
std::string text;
llvm::raw_string_ostream stream(text);
diagnostic.print(stream);
sawLateCarrierDiagnostic |= llvm::StringRef(text).contains(
"hip.constant survived past hip-externalize-constants");
return mlir::success();
});
CapturingFileSystem lateFs;
bool lateOk = true;
runOnnxToHipPipeline(context,
R"mlir(
module {
func.func @main_graph(
%input: tensor<2xf32> {onnx.name = "input"})
-> tensor<2xf32>
attributes {hip_ep_sample.emit_late_constant} {
"onnx.Return"(%input) : (tensor<2xf32>) -> ()
}
}
)mlir",
lateFs, lateOk);
check(!lateOk && sawLateCarrierDiagnostic,
"BeforeBufferization sample carrier is diagnosed");
#endif
// Memory-address carriers are an in-process production contract: the pass
// receives an injected FileSystem while the referenced ORT storage is live.
// Exercise all three consumers with a tiny buffer so this test detects an
// accidental eager copy, source-kind mismatch, or partial-blob offset bug.
std::array<char, 4> hostBytes = {11, 22, 33, 44};
std::string memoryIr =
memorySourceModule(reinterpret_cast<uintptr_t>(hostBytes.data()));
const std::vector<char> expectedBytes(hostBytes.begin(), hostBytes.end());
CapturingFileSystem fullMemoryFs;
bool fullMemoryOk = false;
auto fullMemoryModule =
runMemoryExternalizer(context, memoryIr, fullMemoryFs, /*minElements=*/1,
/*skipData=*/false, fullMemoryOk);
check(fullMemoryOk && fullMemoryModule &&
fullMemoryFs.files["model.constants.bin"] == expectedBytes,
"memory source writes exact full-artifact bytes");
CapturingFileSystem streamingMemoryFs;
bool streamingMemoryOk = false;
auto streamingMemoryModule = runMemoryExternalizer(
context, memoryIr, streamingMemoryFs, /*minElements=*/1,
/*skipData=*/true, streamingMemoryOk);
auto sourceKinds = streamingMemoryModule
? (*streamingMemoryModule)
->getAttrOfType<mlir::DenseI32ArrayAttr>(
"hipdnn.constant_source_kinds")
: nullptr;
auto memoryOffsets = streamingMemoryModule
? (*streamingMemoryModule)
->getAttrOfType<mlir::DenseI64ArrayAttr>(
"hipdnn.constant_mem_offsets")
: nullptr;
check(streamingMemoryOk && streamingMemoryModule && sourceKinds &&
sourceKinds.asArrayRef().size() == 1 &&
sourceKinds.asArrayRef()[0] ==
static_cast<int32_t>(
mlir::hip::ConstantMetadataSourceKind::Memory) &&
memoryOffsets && memoryOffsets.asArrayRef().size() == 1 &&
memoryOffsets.asArrayRef()[0] == 0 &&
streamingMemoryFs.files["model.constants.bin"] == expectedBytes,
"memory source writes exact streaming metadata and partial blob");
CapturingFileSystem inlineMemoryFs;
bool inlineMemoryOk = false;
auto inlineMemoryModule = runMemoryExternalizer(
context, memoryIr, inlineMemoryFs, /*minElements=*/0,
/*skipData=*/false, inlineMemoryOk);
std::vector<char> materializedBytes;
if (inlineMemoryModule)
inlineMemoryModule->walk([&](mlir::arith::ConstantOp op) {
if (auto value = mlir::dyn_cast<mlir::DenseElementsAttr>(op.getValue())) {
auto raw = value.getRawData();
materializedBytes.assign(raw.begin(), raw.end());
}
});
check(inlineMemoryOk && inlineMemoryModule &&
materializedBytes == expectedBytes && inlineMemoryFs.files.empty(),
"memory source materializes exact inline bytes");
if (g_failures == 0) {
llvm::outs() << "\nAll checks passed.\n";
return 0;
}
llvm::errs() << "\n" << g_failures << " check(s) failed.\n";
return 1;
}