Ejemplo n.º 1
0
        /// <summary>
        /// Handles activator operations.
        /// </summary>
        /// <param name="context">The current invocation context.</param>
        /// <returns>The resulting value.</returns>
        private static Value HandleActivator(ref InvocationContext context)
        {
            var location = context.Location;

            var genericArgs = context.GetMethodGenericArguments();

            if (context.Method.Name != nameof(Activator.CreateInstance) ||
                context.NumArguments != 0 ||
                genericArgs.Length != 1 ||
                !genericArgs[0].IsValueType)
            {
                throw context.Location.GetNotSupportedException(
                          ErrorMessages.NotSupportedActivatorOperation,
                          context.Method.Name);
            }

            return(context.Builder.CreateNull(
                       location,
                       context.Builder.CreateType(genericArgs[0])));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles view operations.
        /// </summary>
        /// <param name="context">The current invocation context.</param>
        /// <param name="attribute">The intrinsic attribute.</param>
        /// <returns>The resulting value.</returns>
        private static ValueReference HandleSharedMemoryOperation(
            ref InvocationContext context,
            SharedMemoryIntrinsicAttribute attribute)
        {
            var builder        = context.Builder;
            var location       = context.Location;
            var genericArgs    = context.GetMethodGenericArguments();
            var allocationType = builder.CreateType(genericArgs[0]);

            switch (attribute.IntrinsicKind)
            {
            case SharedMemoryIntrinsicKind.AllocateElement:
                return(builder.CreateAlloca(
                           location,
                           allocationType,
                           MemoryAddressSpace.Shared));

            case SharedMemoryIntrinsicKind.Allocate:
                return(builder.CreateNewView(
                           location,
                           builder.CreateStaticAllocaArray(
                               location,
                               context[0],
                               allocationType,
                               MemoryAddressSpace.Shared),
                           context[0]));

            case SharedMemoryIntrinsicKind.AllocateDynamic:
                var alloca = builder.CreateDynamicAllocaArray(
                    location,
                    allocationType,
                    MemoryAddressSpace.Shared).ResolveAs <Alloca>();
                return(builder.CreateNewView(
                           location,
                           alloca,
                           alloca.ArrayLength));
            }
            throw context.Location.GetNotSupportedException(
                      ErrorMessages.NotSupportedSharedMemoryIntrinsic,
                      attribute.IntrinsicKind.ToString());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Handles local operations.
        /// </summary>
        /// <param name="context">The current invocation context.</param>
        /// <param name="attribute">The intrinsic attribute.</param>
        /// <returns>The resulting value.</returns>
        private static ValueReference HandleLocalMemoryOperation(
            ref InvocationContext context,
            LocalMemoryIntrinsicAttribute attribute)
        {
            var builder        = context.Builder;
            var location       = context.Location;
            var genericArgs    = context.GetMethodGenericArguments();
            var allocationType = builder.CreateType(genericArgs[0]);

            return(attribute.IntrinsicKind == LocalMemoryIntrinsicKind.Allocate
                ? builder.CreateNewView(
                       location,
                       builder.CreateAlloca(
                           location,
                           allocationType,
                           MemoryAddressSpace.Local,
                           context[0]),
                       context[0])
                : throw context.Location.GetNotSupportedException(
                       ErrorMessages.NotSupportedLocalMemoryIntrinsic,
                       attribute.IntrinsicKind.ToString()));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Handles interop operations.
        /// </summary>
        /// <param name="context">The current invocation context.</param>
        /// <param name="attribute">The intrinsic attribute.</param>
        /// <returns>The resulting value.</returns>
        private static ValueReference HandleInterop(
            ref InvocationContext context,
            InteropIntrinsicAttribute attribute)
        {
            var builder = context.Builder;

            return(attribute.IntrinsicKind switch
            {
                InteropIntrinsicKind.SizeOf => builder.CreateSizeOf(
                    context.Location,
                    builder.CreateType(context.GetMethodGenericArguments()[0])),
                InteropIntrinsicKind.OffsetOf => CreateOffsetOf(ref context),
                InteropIntrinsicKind.FloatAsInt => builder.CreateFloatAsIntCast(
                    context.Location,
                    context[0]),
                InteropIntrinsicKind.IntAsFloat => builder.CreateIntAsFloatCast(
                    context.Location,
                    context[0]),
                _ => throw context.Location.GetNotSupportedException(
                    ErrorMessages.NotSupportedInteropIntrinsic,
                    attribute.IntrinsicKind.ToString()),
            });
Ejemplo n.º 5
0
        /// <summary cref="CompilerDeviceFunctions.MakeGrid(InvocationContext, GridIntrinsicKind)"/>
        protected override Value?MakeGrid(InvocationContext context, GridIntrinsicKind kind)
        {
            if (context.GetArgs().Length != 0 || context.GetMethodGenericArguments().Length != 0)
            {
                throw context.CompilationContext.GetNotSupportedException(
                          ErrorMessages.NotSupportedGridIntrinsic, kind);
            }

            Lazy <LLVMValueRef>[] indices;
            switch (kind)
            {
            case GridIntrinsicKind.GetGridDimension:
                indices = GetGridDimensions;
                break;

            case GridIntrinsicKind.GetGroupDimension:
                indices = GetBlockDimensions;
                break;

            default:
                throw context.CompilationContext.GetNotSupportedException(
                          ErrorMessages.NotSupportedGridIntrinsic, kind);
            }

            Debug.Assert(indices != null, "Invalid grid indices");

            var builder    = context.Builder;
            var indexValue = GetUndef(context.Unit.GetType(typeof(Index3)));

            for (int i = 0; i < 3; ++i)
            {
                var resolvedValue = BuildCall(builder, indices[i].Value);
                indexValue = BuildInsertValue(builder, indexValue, resolvedValue, i, string.Empty);
            }

            return(new Value(typeof(Index3), indexValue));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Handles shared memory operations.
        /// </summary>
        /// <param name="context">The current invocation context.</param>
        /// <param name="attribute">The intrinsic attribute.</param>
        /// <returns>The resulting value.</returns>
        private static ValueReference HandleViewOperation(
            ref InvocationContext context,
            ViewIntrinsicAttribute attribute)
        {
            var builder  = context.Builder;
            var location = context.Location;
            // These methods are instance calls -> load instance value
            var paramOffset   = 0;
            var instanceValue = builder.CreateLoad(
                location,
                context[paramOffset++]);

            return(attribute.IntrinsicKind switch
            {
                ViewIntrinsicKind.GetViewLength => builder.CreateGetViewLength(
                    location,
                    instanceValue),
                ViewIntrinsicKind.GetViewLongLength => builder.CreateGetViewLongLength(
                    location,
                    instanceValue),
                ViewIntrinsicKind.GetViewLengthInBytes => builder.CreateArithmetic(
                    location,
                    builder.CreateGetViewLongLength(location, instanceValue),
                    builder.CreateLongSizeOf(
                        location,
                        (instanceValue.Type as AddressSpaceType).ElementType),
                    BinaryArithmeticKind.Mul),
                ViewIntrinsicKind.GetSubView => builder.CreateSubViewValue(
                    location,
                    instanceValue,
                    context[paramOffset++],
                    context[paramOffset++]),
                ViewIntrinsicKind.GetSubViewImplicitLength =>
                builder.CreateSubViewValue(
                    location,
                    instanceValue,
                    context[paramOffset],
                    builder.CreateArithmetic(
                        location,
                        builder.CreateGetViewLongLength(location, instanceValue),
                        context[paramOffset],
                        BinaryArithmeticKind.Sub)),
                ViewIntrinsicKind.GetViewElementAddress =>
                GetViewElementAddress(
                    ref context,
                    instanceValue,
                    context[paramOffset++]),
                ViewIntrinsicKind.CastView => builder.CreateViewCast(
                    location,
                    instanceValue,
                    builder.CreateType(context.GetMethodGenericArguments()[0])),
                ViewIntrinsicKind.IsValidView => builder.CreateCompare(
                    location,
                    builder.CreateGetViewLongLength(location, instanceValue),
                    builder.CreatePrimitiveValue(location, 0L),
                    CompareKind.GreaterThan),
                ViewIntrinsicKind.GetViewExtent => builder.CreateIndex(
                    location,
                    builder.CreateGetViewLength(location, instanceValue)),
                ViewIntrinsicKind.GetViewLongExtent => builder.CreateIndex(
                    location,
                    builder.CreateGetViewLongLength(location, instanceValue)),
                ViewIntrinsicKind.GetViewElementAddressByIndex =>
                GetViewElementAddress(
                    ref context,
                    instanceValue,
                    builder.CreateGetField(
                        location,
                        context[paramOffset++],
                        new FieldAccess(0))),
                ViewIntrinsicKind.GetViewLinearElementAddress =>
                RemapToLinearElementAddress(ref context),
                ViewIntrinsicKind.AsLinearView => instanceValue,
                ViewIntrinsicKind.AlignTo =>
                builder.CreateAlignViewTo(
                    location,
                    instanceValue,
                    context[paramOffset++]),
                _ => throw context.Location.GetNotSupportedException(
                    ErrorMessages.NotSupportedViewIntrinsic,
                    attribute.IntrinsicKind.ToString()),
            });