Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 28dc3bc

Browse files
bettinaheimBettina Heim
andauthored
Temporarily reverting ref count changes for release (#989)
* Revert "Fixing an issue where reference count increases were not applied in certain cases (#987)" This reverts commit ed60c81. * Revert "Splitting out general purpose helpers for branches and loops that yield values (#985)" This reverts commit 34bd1fe. * Revert "Ref count optimizations and some bug fixes (#982)" This reverts commit 9689a96. Co-authored-by: Bettina Heim <[email protected]>
1 parent 89f41c3 commit 28dc3bc

Some content is hidden

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

43 files changed

+1537
-1616
lines changed

src/QsCompiler/QirGeneration/Context.cs

Lines changed: 55 additions & 189 deletions
Large diffs are not rendered by default.

src/QsCompiler/QirGeneration/Interop.cs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,18 @@ private Value[] ProcessArguments(ArgumentTuple arg, IReadOnlyList<Argument> para
152152
return (length, dataArr);
153153
}
154154

155-
IValue[] GetStructItems(Value value, IEnumerable<ResolvedType> itemTypes, bool registerWithScopeManager)
155+
IValue[] GetStructItems(Value value, IEnumerable<ResolvedType> itemTypes)
156156
{
157157
var itemIndex = 0;
158158
Value NextTupleItem()
159159
{
160160
var itemPtr = this.sharedState.CurrentBuilder.GetElementPtr(Types.PointerElementType(value), value, this.PointerIndex(itemIndex++));
161161
return this.sharedState.CurrentBuilder.Load(Types.PointerElementType(itemPtr), itemPtr);
162162
}
163-
return itemTypes.Select(arg => ProcessGivenValue(arg, registerWithScopeManager, NextTupleItem)).ToArray();
163+
return itemTypes.Select(arg => ProcessGivenValue(arg, NextTupleItem)).ToArray();
164164
}
165165

166-
IValue ProcessGivenValue(ResolvedType type, bool registerWithScopeManager, Func<Value> next)
166+
IValue ProcessGivenValue(ResolvedType type, Func<Value> next)
167167
{
168168
if (type.Resolution.IsUnitType)
169169
{
@@ -174,19 +174,15 @@ IValue ProcessGivenValue(ResolvedType type, bool registerWithScopeManager, Func<
174174
if (type.Resolution is ResolvedTypeKind.ArrayType arrItemType)
175175
{
176176
var (length, dataArr) = LoadSizedArray(givenValue);
177-
ArrayValue array = this.sharedState.Values.CreateArray(length, arrItemType.Item, registerWithScopeManager: false);
178-
if (registerWithScopeManager)
179-
{
180-
this.sharedState.ScopeMgr.RegisterValue(array);
181-
}
177+
ArrayValue array = this.sharedState.Values.CreateArray(length, arrItemType.Item);
182178

183179
var dataArrStart = this.sharedState.CurrentBuilder.PointerToInt(dataArr, this.sharedState.Context.Int64Type);
184180
var givenArrElementType = this.MapToInteropType(array.LlvmElementType) ?? this.sharedState.Values.Unit.LlvmType;
185181
var givenArrElementSize = this.sharedState.ComputeSizeForType(givenArrElementType);
186182

187183
void PopulateItem(Value index)
188184
{
189-
var element = ProcessGivenValue(array.QSharpElementType, false, () =>
185+
var element = ProcessGivenValue(array.QSharpElementType, () =>
190186
{
191187
var offset = this.sharedState.CurrentBuilder.Mul(index, givenArrElementSize);
192188
var elementPointer = this.sharedState.CurrentBuilder.IntToPointer(
@@ -204,30 +200,24 @@ void PopulateItem(Value index)
204200
}
205201
else if (type.Resolution is ResolvedTypeKind.TupleType items)
206202
{
207-
var tupleItems = GetStructItems(givenValue, items.Item, registerWithScopeManager);
208-
return this.sharedState.Values.CreateTuple(registerWithScopeManager, tupleItems);
203+
var tupleItems = GetStructItems(givenValue, items.Item);
204+
return this.sharedState.Values.CreateTuple(tupleItems);
209205
}
210206
else if (type.Resolution.IsBigInt)
211207
{
212208
var createBigInt = this.sharedState.GetOrCreateRuntimeFunction(RuntimeLibrary.BigIntCreateArray);
213209
var (length, dataArr) = LoadSizedArray(givenValue);
214210
var argValue = this.sharedState.CurrentBuilder.Call(createBigInt, length, dataArr);
215211
var value = this.sharedState.Values.From(argValue, type);
216-
if (registerWithScopeManager)
217-
{
218-
this.sharedState.ScopeMgr.RegisterValue(value);
219-
}
212+
this.sharedState.ScopeMgr.RegisterValue(value);
220213
return value;
221214
}
222215
else if (type.Resolution.IsString)
223216
{
224217
var createString = this.sharedState.GetOrCreateRuntimeFunction(RuntimeLibrary.StringCreate);
225218
var argValue = this.sharedState.CurrentBuilder.Call(createString, givenValue);
226219
var value = this.sharedState.Values.From(argValue, type);
227-
if (registerWithScopeManager)
228-
{
229-
this.sharedState.ScopeMgr.RegisterValue(value);
230-
}
220+
this.sharedState.ScopeMgr.RegisterValue(value);
231221
return value;
232222
}
233223
else if (type.Resolution.IsResult)
@@ -247,7 +237,7 @@ void PopulateItem(Value index)
247237
else if (type.Resolution.IsRange)
248238
{
249239
var itemTypes = Enumerable.Repeat(ResolvedType.New(ResolvedTypeKind.Int), 3);
250-
var rangeItems = GetStructItems(givenValue, itemTypes, registerWithScopeManager);
240+
var rangeItems = GetStructItems(givenValue, itemTypes);
251241
return this.sharedState.CreateRange(rangeItems[0].Value, rangeItems[1].Value, rangeItems[2].Value);
252242
}
253243
else if (givenValue.NativeType.IsInteger)
@@ -279,7 +269,7 @@ IValue ProcessArgumentTupleItem(ArgumentTuple item, Func<Value> nextArgument)
279269
else
280270
{
281271
return item is ArgumentTuple.QsTupleItem innerItem
282-
? ProcessGivenValue(innerItem.Item.Type, true, nextArgument)
272+
? ProcessGivenValue(innerItem.Item.Type, nextArgument)
283273
: throw new NotSupportedException("unknown item in argument tuple");
284274
}
285275
}
@@ -362,7 +352,7 @@ void PopulateItem(Value index)
362352
var end = this.sharedState.CurrentBuilder.Sub(array.Length, this.sharedState.Context.CreateConstant(1L));
363353
this.sharedState.IterateThroughRange(start, null, end, PopulateItem);
364354

365-
var tupleItems = new[] { array.Length, dataArr };
355+
var tupleItems = new[] { array.Length, dataArr }; // FIXME: CAST DATA ARR TO THE RIGHT TYPE IF NEEDED
366356
var mappedType = this.MapToInteropType(array.QSharpType)!;
367357
return PopulateStruct((IPointerType)mappedType, tupleItems);
368358
}

src/QsCompiler/QirGeneration/QIR/DataStructures.cs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ private Value AllocateTuple(bool registerWithScopeManager)
345345
var tuple = this.sharedState.CurrentBuilder.Call(constructor, size);
346346
if (registerWithScopeManager)
347347
{
348-
this.sharedState.ScopeMgr.RegisterValue(this, shallow: true);
348+
this.sharedState.ScopeMgr.RegisterValue(this);
349349
}
350350
return tuple;
351351
}
@@ -469,7 +469,7 @@ private Value AllocateArray(bool registerWithScopeManager)
469469
var pointer = this.sharedState.CurrentBuilder.Call(constructor, elementSize, this.Length);
470470
if (registerWithScopeManager)
471471
{
472-
this.sharedState.ScopeMgr.RegisterValue(this, shallow: true);
472+
this.sharedState.ScopeMgr.RegisterValue(this);
473473
}
474474
return pointer;
475475
}
@@ -536,34 +536,6 @@ internal class CallableValue : IValue
536536

537537
public ResolvedType QSharpType { get; }
538538

539-
/// <summary>
540-
/// Creates a callable value of the given type and registers it with the scope manager.
541-
/// The necessary functions to invoke the callable are defined by the callable table;
542-
/// i.e. the globally defined array of function pointers accessible via the given global variable.
543-
/// </summary>
544-
/// <param name="callableType">The Q# type of the callable value.</param>
545-
/// <param name="table">The global variable that contains the array of function pointers defining the callable.</param>
546-
/// <param name="context">Generation context where constants are defined and generated if needed.</param>
547-
/// <param name="captured">All captured values.</param>
548-
internal CallableValue(ResolvedType callableType, GlobalVariable table, GenerationContext context, ImmutableArray<TypedExpression>? captured = null)
549-
{
550-
this.LlvmType = context.Types.Callable;
551-
this.QSharpType = callableType;
552-
553-
// The runtime function CallableCreate creates a new value with reference count 1.
554-
var createCallable = context.GetOrCreateRuntimeFunction(RuntimeLibrary.CallableCreate);
555-
var capture = captured == null || captured.Value.Length == 0 ? null : context.Values.CreateTuple(captured.Value);
556-
var memoryManagementTable = context.GetOrCreateCallableMemoryManagementTable(capture);
557-
this.Value = context.CurrentBuilder.Call(createCallable, table, memoryManagementTable, capture?.OpaquePointer ?? context.Constants.UnitValue);
558-
context.ScopeMgr.RegisterValue(this, shallow: true);
559-
}
560-
561-
/// <summary>
562-
/// Creates a new callable value of the given type.
563-
/// </summary>
564-
/// <param name="value">The pointer to a QIR callable value.</param>
565-
/// <param name="type">Q# type of the callable.</param>
566-
/// <param name="context">Generation context where constants are defined and generated if needed.</param>
567539
internal CallableValue(Value value, ResolvedType type, GenerationContext context)
568540
{
569541
this.Value = value;

src/QsCompiler/QirGeneration/QIR/Values.cs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,24 @@ internal TupleValue CreateTuple(ImmutableArray<ResolvedType> elementTypes, bool
113113
/// <param name="tupleElements">The tuple elements</param>
114114
internal TupleValue CreateTuple(ImmutableArray<TypedExpression> tupleElements, bool registerWithScopeManager = true)
115115
{
116-
var elements = tupleElements.Select(this.sharedState.EvaluateSubexpression).ToArray();
117-
return this.CreateTuple(null, registerWithScopeManager, elements);
116+
var elementTypes = tupleElements.Select(v => v.ResolvedType).ToImmutableArray();
117+
TupleValue tuple = new TupleValue(elementTypes, this.sharedState, registerWithScopeManager);
118+
PointerValue[] itemPointers = tuple.GetTupleElementPointers();
119+
120+
var elements = tupleElements.Select(this.sharedState.BuildSubitem).ToArray();
121+
for (var i = 0; i < itemPointers.Length; ++i)
122+
{
123+
itemPointers[i].StoreValue(elements[i]);
124+
}
125+
126+
return tuple;
118127
}
119128

120129
/// <summary>
121130
/// Builds a tuple with the items set to the given tuple elements.
122131
/// The tuple represents a value of user defined type if a name is specified.
123132
/// Registers the value with the scope manager, unless registerWithScopeManager is set to false.
124-
/// Does *not* increase the reference count for the tuple elements.
133+
/// Increases the reference count for the tuple elements.
125134
/// </summary>
126135
/// <param name="typeName">The name of the user defined typed that the tuple represents</param>
127136
/// <param name="registerWithScopeManager">Whether or not to register the built tuple with the scope manager</param>
@@ -135,6 +144,7 @@ private TupleValue CreateTuple(UserDefinedType? typeName, bool registerWithScope
135144
for (var i = 0; i < itemPointers.Length; ++i)
136145
{
137146
itemPointers[i].StoreValue(tupleElements[i]);
147+
this.sharedState.ScopeMgr.IncreaseReferenceCount(tupleElements[i]);
138148
}
139149

140150
return tuple;
@@ -143,7 +153,7 @@ private TupleValue CreateTuple(UserDefinedType? typeName, bool registerWithScope
143153
/// <summary>
144154
/// Builds a tuple with the items set to the given tuple elements.
145155
/// Registers the value with the scope manager, unless registerWithScopeManager is set to false.
146-
/// Does *not* increase the reference count for the tuple elements.
156+
/// Increases the reference count for the tuple elements.
147157
/// </summary>
148158
/// <param name="registerWithScopeManager">Whether or not to register the built tuple with the scope manager</param>
149159
/// <param name="tupleElements">The tuple elements</param>
@@ -153,16 +163,16 @@ internal TupleValue CreateTuple(bool registerWithScopeManager, params IValue[] t
153163
/// <summary>
154164
/// Builds a tuple with the items set to the given tuple elements.
155165
/// Registers the value with the scope manager.
156-
/// Does *not* increase the reference count for the tuple elements.
166+
/// Increases the reference count for the tuple elements.
157167
/// </summary>
158168
/// <param name="tupleElements">The tuple elements</param>
159169
internal TupleValue CreateTuple(params IValue[] tupleElements) =>
160-
this.CreateTuple(null, true, tupleElements);
170+
this.CreateTuple(true, tupleElements);
161171

162172
/// <summary>
163173
/// Builds a tuple representing a Q# value of user defined type with the items set to the given elements.
164174
/// Registers the value with the scope manager, unless registerWithScopeManager is set to false.
165-
/// Does *not* increase the reference count for the tuple elements.
175+
/// Increases the reference count for the tuple elements.
166176
/// </summary>
167177
/// <param name="typeName">The name of the user defined type</param>
168178
/// <param name="registerWithScopeManager">Whether or not to register the built tuple with the scope manager</param>
@@ -173,7 +183,7 @@ internal TupleValue CreateCustomType(UserDefinedType typeName, bool registerWith
173183
/// <summary>
174184
/// Builds a tuple representing a Q# value of user defined type with the items set to the given elements.
175185
/// Registers the value with the scope manager.
176-
/// Does *not* increase the reference count for the tuple elements.
186+
/// Increases the reference count for the tuple elements.
177187
/// </summary>
178188
/// <param name="typeName">The name of the user defined type</param>
179189
/// <param name="tupleElements">The tuple elements</param>
@@ -196,14 +206,22 @@ internal ArrayValue CreateArray(Value length, ResolvedType elementType, bool reg
196206
/// <param name="arrayElements">The elements in the array</param>
197207
internal ArrayValue CreateArray(ResolvedType elementType, ImmutableArray<TypedExpression> arrayElements, bool registerWithScopeManager = true)
198208
{
199-
var elements = arrayElements.Select(this.sharedState.EvaluateSubexpression).ToArray();
200-
return this.CreateArray(elementType, registerWithScopeManager, elements);
209+
var array = new ArrayValue((uint)arrayElements.Length, elementType, this.sharedState, registerWithScopeManager);
210+
var itemPointers = array.GetArrayElementPointers();
211+
212+
var elements = arrayElements.Select(this.sharedState.BuildSubitem).ToArray();
213+
for (var i = 0; i < itemPointers.Length; ++i)
214+
{
215+
itemPointers[i].StoreValue(elements[i]);
216+
}
217+
218+
return array;
201219
}
202220

203221
/// <summary>
204222
/// Builds an array that containsthe given array elements.
205223
/// Registers the value with the scope manager, unless registerWithScopeManager is set to false.
206-
/// Does *not* increase the reference count for the array elements.
224+
/// Increases the reference count for the array elements.
207225
/// </summary>
208226
/// <param name="elementType">The Q# type of the array elements</param>
209227
/// <param name="registerWithScopeManager">Whether or not to register the built tuple with the scope manager</param>
@@ -216,6 +234,7 @@ internal ArrayValue CreateArray(ResolvedType elementType, bool registerWithScope
216234
for (var i = 0; i < itemPointers.Length; ++i)
217235
{
218236
itemPointers[i].StoreValue(arrayElements[i]);
237+
this.sharedState.ScopeMgr.IncreaseReferenceCount(arrayElements[i]);
219238
}
220239

221240
return array;
@@ -224,21 +243,10 @@ internal ArrayValue CreateArray(ResolvedType elementType, bool registerWithScope
224243
/// <summary>
225244
/// Builds an array that containsthe given array elements.
226245
/// Registers the value with the scope manager.
227-
/// Does *not* increase the reference count for the array elements.
246+
/// Increases the reference count for the array elements.
228247
/// </summary>
229248
/// <param name="arrayElements">The elements in the array</param>
230249
internal ArrayValue CreateArray(ResolvedType elementType, params IValue[] arrayElements) =>
231250
this.CreateArray(elementType, true, arrayElements);
232-
233-
/// <summary>
234-
/// Creates a callable value of the given type and registers it with the scope manager.
235-
/// The necessary functions to invoke the callable are defined by the callable table;
236-
/// i.e. the globally defined array of function pointers accessible via the given global variable.
237-
/// </summary>
238-
/// <param name="callableType">The Q# type of the callable value.</param>
239-
/// <param name="table">The global variable that contains the array of function pointers defining the callable.</param>
240-
/// <param name="captured">All captured values.</param>
241-
internal CallableValue CreateCallable(ResolvedType callableType, GlobalVariable table, ImmutableArray<TypedExpression>? captured = null) =>
242-
new CallableValue(callableType, table, this.sharedState, captured);
243251
}
244252
}

0 commit comments

Comments
 (0)