Skip to content

Commit 4c6bfe8

Browse files
Merge branch 'main' into warn-draw
2 parents b05e106 + 5fad63e commit 4c6bfe8

40 files changed

+983
-179
lines changed

.github/workflows/config/validation_config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"operating_systems":
77
[
88
"ubuntu:22.04",
9-
"redhat/ubi8:8.10",
109
"fedora:42"
1110
]
1211
},

include/cudaq/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88

99
include(HandleLLVMOptions)
1010
add_subdirectory(Optimizer)
11+
add_subdirectory(Support)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# ============================================================================ #
2+
# Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. #
3+
# All rights reserved. #
4+
# #
5+
# This source code and the accompanying materials are made available under #
6+
# the terms of the Apache License 2.0 which accompanies this distribution. #
7+
# ============================================================================ #
8+
9+
# Install TargetConfig.h file
10+
install (FILES TargetConfig.h DESTINATION include/cudaq/Support)

include/cudaq/Support/TargetConfig.h

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#pragma once
1010

11-
#include "llvm/Support/YAMLTraits.h"
1211
#include <optional>
1312
#include <string>
1413
#include <vector>
@@ -142,56 +141,3 @@ std::string processRuntimeArgs(const TargetConfig &config,
142141
const std::vector<std::string> &targetArgv);
143142
} // namespace config
144143
} // namespace cudaq
145-
146-
// These structs can be used in a vector.
147-
LLVM_YAML_IS_SEQUENCE_VECTOR(cudaq::config::TargetFeatureFlag)
148-
LLVM_YAML_IS_SEQUENCE_VECTOR(cudaq::config::TargetArgument)
149-
LLVM_YAML_IS_SEQUENCE_VECTOR(cudaq::config::ConditionalBuildConfig)
150-
LLVM_YAML_IS_SEQUENCE_VECTOR(cudaq::config::BackendFeatureMap)
151-
152-
namespace llvm {
153-
namespace yaml {
154-
// YML serialization declarations.
155-
template <>
156-
struct ScalarBitSetTraits<cudaq::config::TargetFeatureFlag> {
157-
static void bitset(IO &io, cudaq::config::TargetFeatureFlag &value);
158-
};
159-
160-
template <>
161-
struct ScalarEnumerationTraits<cudaq::config::ArgumentType> {
162-
static void enumeration(IO &io, cudaq::config::ArgumentType &value);
163-
};
164-
165-
template <>
166-
struct MappingTraits<cudaq::config::TargetArgument> {
167-
static void mapping(IO &io, cudaq::config::TargetArgument &info);
168-
};
169-
170-
template <>
171-
struct BlockScalarTraits<cudaq::config::SimulationBackendSetting> {
172-
static void output(const cudaq::config::SimulationBackendSetting &Value,
173-
void *Ctxt, llvm::raw_ostream &OS);
174-
static StringRef input(StringRef Scalar, void *Ctxt,
175-
cudaq::config::SimulationBackendSetting &Value);
176-
};
177-
template <>
178-
struct MappingTraits<cudaq::config::ConditionalBuildConfig> {
179-
static void mapping(IO &io, cudaq::config::ConditionalBuildConfig &info);
180-
};
181-
182-
template <>
183-
struct MappingTraits<cudaq::config::BackendEndConfigEntry> {
184-
static void mapping(IO &io, cudaq::config::BackendEndConfigEntry &info);
185-
};
186-
template <>
187-
struct MappingTraits<cudaq::config::BackendFeatureMap> {
188-
static void mapping(IO &io, cudaq::config::BackendFeatureMap &info);
189-
};
190-
191-
template <>
192-
struct MappingTraits<cudaq::config::TargetConfig> {
193-
static void mapping(IO &io, cudaq::config::TargetConfig &info);
194-
};
195-
196-
} // namespace yaml
197-
} // namespace llvm
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/****************************************************************-*- C++ -*-****
2+
* Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. *
3+
* All rights reserved. *
4+
* *
5+
* This source code and the accompanying materials are made available under *
6+
* the terms of the Apache License 2.0 which accompanies this distribution. *
7+
******************************************************************************/
8+
9+
#pragma once
10+
11+
#include "TargetConfig.h"
12+
#include "llvm/Support/YAMLTraits.h"
13+
14+
// These structs can be used in a vector.
15+
LLVM_YAML_IS_SEQUENCE_VECTOR(cudaq::config::TargetFeatureFlag)
16+
LLVM_YAML_IS_SEQUENCE_VECTOR(cudaq::config::TargetArgument)
17+
LLVM_YAML_IS_SEQUENCE_VECTOR(cudaq::config::ConditionalBuildConfig)
18+
LLVM_YAML_IS_SEQUENCE_VECTOR(cudaq::config::BackendFeatureMap)
19+
20+
namespace llvm {
21+
namespace yaml {
22+
// YML serialization declarations.
23+
template <>
24+
struct ScalarBitSetTraits<cudaq::config::TargetFeatureFlag> {
25+
static void bitset(IO &io, cudaq::config::TargetFeatureFlag &value);
26+
};
27+
28+
template <>
29+
struct ScalarEnumerationTraits<cudaq::config::ArgumentType> {
30+
static void enumeration(IO &io, cudaq::config::ArgumentType &value);
31+
};
32+
33+
template <>
34+
struct MappingTraits<cudaq::config::TargetArgument> {
35+
static void mapping(IO &io, cudaq::config::TargetArgument &info);
36+
};
37+
38+
template <>
39+
struct BlockScalarTraits<cudaq::config::SimulationBackendSetting> {
40+
static void output(const cudaq::config::SimulationBackendSetting &Value,
41+
void *Ctxt, llvm::raw_ostream &OS);
42+
static StringRef input(StringRef Scalar, void *Ctxt,
43+
cudaq::config::SimulationBackendSetting &Value);
44+
};
45+
template <>
46+
struct MappingTraits<cudaq::config::ConditionalBuildConfig> {
47+
static void mapping(IO &io, cudaq::config::ConditionalBuildConfig &info);
48+
};
49+
50+
template <>
51+
struct MappingTraits<cudaq::config::BackendEndConfigEntry> {
52+
static void mapping(IO &io, cudaq::config::BackendEndConfigEntry &info);
53+
};
54+
template <>
55+
struct MappingTraits<cudaq::config::BackendFeatureMap> {
56+
static void mapping(IO &io, cudaq::config::BackendFeatureMap &info);
57+
};
58+
59+
template <>
60+
struct MappingTraits<cudaq::config::TargetConfig> {
61+
static void mapping(IO &io, cudaq::config::TargetConfig &info);
62+
};
63+
64+
} // namespace yaml
65+
} // namespace llvm

lib/Optimizer/Builder/Marshal.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,8 @@ cudaq::opt::marshal::dropAnyHiddenArguments(MutableArrayRef<BlockArgument> args,
785785
std::pair<bool, func::FuncOp> cudaq::opt::marshal::lookupHostEntryPointFunc(
786786
StringRef mangledEntryPointName, ModuleOp module, func::FuncOp funcOp) {
787787
if (mangledEntryPointName == "BuilderKernel.EntryPoint" ||
788-
mangledEntryPointName.contains("_PyKernelEntryPointRewrite")) {
788+
mangledEntryPointName.contains("_PyKernelEntryPointRewrite") ||
789+
funcOp.empty()) {
789790
// No host entry point needed.
790791
return {false, func::FuncOp{}};
791792
}

lib/Optimizer/Transforms/GenKernelExecution.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -869,14 +869,13 @@ class GenerateKernelExecution
869869
for (auto &op : *module.getBody())
870870
if (auto funcOp = dyn_cast<func::FuncOp>(op))
871871
if (funcOp.getName().startswith(cudaq::runtime::cudaqGenPrefixName) &&
872-
cudaq::opt::marshal::hasLegalType(funcOp.getFunctionType()))
872+
cudaq::opt::marshal::hasLegalType(funcOp.getFunctionType()) &&
873+
!funcOp.empty() && !funcOp->hasAttr(cudaq::generatorAnnotation))
873874
workList.push_back(funcOp);
874875

875876
LLVM_DEBUG(llvm::dbgs()
876877
<< workList.size() << " kernel entry functions to process\n");
877878
for (auto funcOp : workList) {
878-
if (funcOp->hasAttr(cudaq::generatorAnnotation))
879-
continue;
880879
auto loc = funcOp.getLoc();
881880
[[maybe_unused]] auto className =
882881
funcOp.getName().drop_front(cudaq::runtime::cudaqGenPrefixLength);

lib/Optimizer/Transforms/LowerToCFG.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,10 @@ class RewriteScope : public OpRewritePattern<cudaq::cc::ScopeOp> {
5757
auto loc = scopeOp.getLoc();
5858
auto *initBlock = rewriter.getInsertionBlock();
5959
Value stacksave;
60-
auto module = scopeOp.getOperation()->getParentOfType<ModuleOp>();
6160
auto ptrTy = cudaq::cc::PointerType::get(rewriter.getI8Type());
6261
if (scopeOp.hasAllocation(/*quantumAllocs=*/false)) {
63-
auto fun = cudaq::opt::factory::createFunction(
64-
"llvm.stacksave", ArrayRef<Type>{ptrTy}, {}, module);
65-
fun.setPrivate();
6662
auto call = rewriter.create<func::CallOp>(
67-
loc, ptrTy, fun.getSymNameAttr(), ArrayRef<Value>{});
63+
loc, ptrTy, cudaq::llvmStackSave, ArrayRef<Value>{});
6864
stacksave = call.getResult(0);
6965
}
7066
auto initPos = rewriter.getInsertionPoint();
@@ -93,10 +89,8 @@ class RewriteScope : public OpRewritePattern<cudaq::cc::ScopeOp> {
9389
rewriter.inlineRegionBefore(scopeOp.getInitRegion(), endBlock);
9490
if (stacksave) {
9591
rewriter.setInsertionPointToStart(endBlock);
96-
auto fun = cudaq::opt::factory::createFunction(
97-
"llvm.stackrestore", {}, ArrayRef<Type>{ptrTy}, module);
98-
fun.setPrivate();
99-
rewriter.create<func::CallOp>(loc, ArrayRef<Type>{}, fun.getSymNameAttr(),
92+
rewriter.create<func::CallOp>(loc, ArrayRef<Type>{},
93+
cudaq::llvmStackRestore,
10094
ArrayRef<Value>{stacksave});
10195
}
10296
rewriter.replaceOp(scopeOp, scopeResults);
@@ -331,10 +325,6 @@ class ConvertToCFGPrep
331325
mod.emitError("could not load llvm.stacksave intrinsic.");
332326
signalPassFailure();
333327
}
334-
if (failed(irBuilder.loadIntrinsic(mod, cudaq::llvmStackRestore))) {
335-
mod.emitError("could not load llvm.stackrestore intrinsic.");
336-
signalPassFailure();
337-
}
338328
}
339329
};
340330
} // namespace

lib/Support/Config/TargetConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* the terms of the Apache License 2.0 which accompanies this distribution. *
77
******************************************************************************/
88

9-
#include "cudaq/Support/TargetConfig.h"
9+
#include "cudaq/Support/TargetConfigYaml.h"
1010
#include "llvm/Support/Allocator.h"
1111
#include "llvm/Support/Base64.h"
1212
#include "llvm/Support/CommandLine.h"

python/cudaq/kernel/captured_data.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
# ============================================================================ #
88

99
import uuid
10-
import numpy as np
10+
import weakref
1111

12+
import numpy as np
1213
from cudaq.mlir._mlir_libs._quakeDialects import cudaq_runtime
1314
from cudaq.mlir.dialects import arith, cc, func
1415
from cudaq.mlir.ir import (ComplexType, F32Type, F64Type, FlatSymbolRefAttr,
@@ -34,20 +35,29 @@ def __init__(self, **kwargs):
3435
self.loc = kwargs['loc'] if 'loc' in kwargs else None
3536
self.name = kwargs['name'] if 'name' in kwargs else None
3637
self.module = kwargs['module'] if 'module' in kwargs else None
38+
self._finalizer = weakref.finalize(self, CapturedDataStorage._cleanup,
39+
self.cudaqStateIDs, self.arrayIDs)
40+
41+
@staticmethod
42+
def _cleanup(state_ids, array_ids):
43+
"""
44+
Safely clean up resources associated with captured data during garbage collection.
45+
This method is to be used with `weakref.finalize()` as an alternative to `__del__`,
46+
such that it handles Python interpreter shutdown gracefully, catching exceptions
47+
that occur when modules are unloaded before they are cleaned up.
48+
"""
49+
try:
50+
cudaq_runtime.deletePointersToCudaqState(state_ids)
51+
cudaq_runtime.deletePointersToStateData(array_ids)
52+
except (ImportError, AttributeError):
53+
pass
3754

3855
def setKernelContext(self, ctx, loc, name, module):
3956
self.ctx = ctx
4057
self.loc = loc
4158
self.name = name
4259
self.module = module
4360

44-
def __del__(self):
45-
"""
46-
Remove pointers to stored data for the current kernel.
47-
"""
48-
cudaq_runtime.deletePointersToCudaqState(self.cudaqStateIDs)
49-
cudaq_runtime.deletePointersToStateData(self.arrayIDs)
50-
5161
def getIntegerAttr(self, type, value):
5262
"""
5363
Return an MLIR Integer Attribute of the given IntegerType.

0 commit comments

Comments
 (0)