Пример #1
0
        /// <summary>
        /// Constructs a new type generator and defines all required types
        /// in OpenCL during construction.
        /// </summary>
        /// <param name="typeContext">The associated type context.</param>
        /// <param name="capabilities">The supported capabilities.</param>
        internal CLTypeGenerator(
            IRTypeContext typeContext,
            CLCapabilityContext capabilities)
        {
            TypeContext  = typeContext;
            Capabilities = capabilities;

            // Declare primitive types
            mapping[typeContext.VoidType]   = "void";
            mapping[typeContext.StringType] = "__constant char*";

            foreach (var basicValueType in IRTypeContext.BasicValueTypes)
            {
                if (basicValueType == BasicValueType.Float64 &&
                    TypeContext.MathMode == MathMode.Fast32BitOnly)
                {
                    continue;
                }
                else if (basicValueType == BasicValueType.Float16 &&
                         !capabilities.Float16)
                {
                    continue;
                }
                else if (basicValueType == BasicValueType.Float64 &&
                         !capabilities.Float64)
                {
                    continue;
                }

                var primitiveType = typeContext.GetPrimitiveType(basicValueType);
                mapping[primitiveType] = GetBasicValueType(basicValueType);
            }
        }
Пример #2
0
        /// <summary>
        /// Computes the address of a single element in the scope of a view or a pointer.
        /// </summary>
        /// <param name="location">The current location.</param>
        /// <param name="source">The source view.</param>
        /// <param name="elementIndex">The element index to load.</param>
        /// <returns>A node that represents the element address.</returns>
        public ValueReference CreateLoadElementAddress(
            Location location,
            Value source,
            Value elementIndex)
        {
            // Remove unnecessary pointer casts
            if (elementIndex is IntAsPointerCast cast)
            {
                elementIndex = cast.Value;
            }

            // Assert a valid indexing type from here on
            location.Assert(
                IRTypeContext.IsViewIndexType(elementIndex.BasicValueType));
            var addressSpaceType = source.Type as AddressSpaceType;

            location.AssertNotNull(addressSpaceType);

            // Fold primitive pointer arithmetic that does not change anything
            return(source.Type is PointerType && elementIndex.IsPrimitive(0)
                ? (ValueReference)source
                : Append(new LoadElementAddress(
                             GetInitializer(location),
                             source,
                             elementIndex)));
        }
Пример #3
0
 /// <summary>
 /// Constructs a new OpenCL ABI
 /// </summary>
 /// <param name="typeContext">The current type context.</param>
 /// <param name="targetPlatform">The target platform</param>
 public CLABI(
     IRTypeContext typeContext,
     TargetPlatform targetPlatform)
     : base(typeContext, targetPlatform)
 {
     // Bools are mapped to bytes in OpenCL
     DefineBasicTypeInformation(BasicValueType.Int1, 1);
 }
Пример #4
0
 /// <summary>
 /// Constructs a new PTX ABI
 /// </summary>
 /// <param name="typeContext">The current type context.</param>
 /// <param name="targetPlatform">The target platform</param>
 public PTXABI(
     IRTypeContext typeContext,
     TargetPlatform targetPlatform)
     : base(typeContext, targetPlatform)
 {
     // Bools are mapped to 32bit int registers in PTX
     DefineBasicTypeInformation(BasicValueType.Int1, 4);
 }
Пример #5
0
        /// <summary>
        /// Constructs a new ILGPU main context
        /// </summary>
        /// <param name="builder">The parent builder instance.</param>
        /// <param name="devices">The array of accelerator descriptions.</param>
        internal Context(
            Builder builder,
            ImmutableArray <Device> devices)
        {
            InstanceId     = InstanceId.CreateNew();
            TargetPlatform = Backend.RuntimePlatform;
            RuntimeSystem  = new RuntimeSystem();
            Properties     = builder.InstantiateProperties();

            // Initialize verifier
            Verifier = builder.EnableVerifier ? Verifier.Instance : Verifier.Empty;

            // Initialize main contexts
            TypeContext = new IRTypeContext(this);
            IRContext   = new IRContext(this);

            // Initialize intrinsic manager
            IntrinsicManager = builder.IntrinsicManager;

            // Create frontend
            DebugInformationManager frontendDebugInformationManager =
                Properties.DebugSymbolsMode > DebugSymbolsMode.Disabled
                ? DebugInformationManager
                : null;

            ILFrontend = builder.EnableParallelCodeGenerationInFrontend
                ? new ILFrontend(this, frontendDebugInformationManager)
                : new ILFrontend(this, frontendDebugInformationManager, 1);

            // Create default IL backend
            DefautltILBackend = new DefaultILBackend(this);

            // Initialize default transformer
            ContextTransformer = Optimizer.CreateTransformer(
                Properties.OptimizationLevel,
                TransformerConfiguration.Transformed,
                Properties.InliningMode);

            // Initialize all devices
            Devices = devices;
            if (devices.IsDefaultOrEmpty)
            {
                // Add a default CPU device
                Devices = ImmutableArray.Create <Device>(CPUDevice.Default);
            }

            // Create a mapping
            deviceMapping = new Dictionary <AcceleratorType, List <Device> >(Devices.Length);
            foreach (var device in Devices)
            {
                if (!deviceMapping.TryGetValue(device.AcceleratorType, out var devs))
                {
                    devs = new List <Device>(8);
                    deviceMapping.Add(device.AcceleratorType, devs);
                }
                devs.Add(device);
            }
        }
Пример #6
0
 /// <summary>
 /// Constructs a new ABI instance.
 /// </summary>
 /// <param name="typeContext">The current type context.</param>
 /// <param name="targetPlatform">The target platform</param>
 public ViewImplementationABI(
     IRTypeContext typeContext,
     TargetPlatform targetPlatform)
     : base(typeContext, targetPlatform,
            pointerSize => new ABITypeInfo(
                ImmutableArray.Create(0, pointerSize),
                pointerSize,
                2 * pointerSize))
 {
 }
Пример #7
0
        /// <summary>
        /// Constructs a new ILGPU main context
        /// </summary>
        /// <param name="optimizationLevel">The optimization level.</param>
        /// <param name="flags">The context flags.</param>
        public Context(ContextFlags flags, OptimizationLevel optimizationLevel)
        {
            // Enable debug information automatically when a debugger is attached
            if (Debugger.IsAttached)
            {
                flags |= DefaultDebug;
            }

            InstanceId        = InstanceId.CreateNew();
            OptimizationLevel = optimizationLevel;
            Flags             = flags.Prepare();
            TargetPlatform    = Backend.RuntimePlatform;
            RuntimeSystem     = new RuntimeSystem();

            // Initialize enhanced PTX backend feature flags
            if (optimizationLevel > OptimizationLevel.O1 &&
                !Flags.HasFlags(ContextFlags.DefaultPTXBackendFeatures))
            {
                Flags |= ContextFlags.EnhancedPTXBackendFeatures;
            }

            // Initialize verifier
            Verifier = flags.HasFlags(ContextFlags.EnableVerifier)
                ? Verifier.Instance
                : Verifier.Empty;

            // Initialize main contexts
            TypeContext = new IRTypeContext(this);
            IRContext   = new IRContext(this);

            // Create frontend
            DebugInformationManager frontendDebugInformationManager =
                HasFlags(ContextFlags.EnableDebugSymbols)
                ? DebugInformationManager
                : null;

            ILFrontend = HasFlags(ContextFlags.EnableParallelCodeGenerationInFrontend)
                ? new ILFrontend(this, frontendDebugInformationManager)
                : new ILFrontend(this, frontendDebugInformationManager, 1);

            // Create default IL backend
            DefautltILBackend = new DefaultILBackend(this);

            // Initialize default transformer
            ContextTransformer = Optimizer.CreateTransformer(
                OptimizationLevel,
                TransformerConfiguration.Transformed,
                Flags);

            // Intrinsics
            IntrinsicManager = new IntrinsicImplementationManager();
            InitIntrinsics();
        }
Пример #8
0
        /// <summary>
        /// Constructs a new view from a pointer and a length.
        /// </summary>
        /// <param name="location">The current location.</param>
        /// <param name="pointer">The source pointer.</param>
        /// <param name="length">The length.</param>
        /// <returns>A node that represents the created view.</returns>
        public ValueReference CreateNewView(
            Location location,
            Value pointer,
            Value length)
        {
            location.Assert(
                IRTypeContext.IsViewIndexType(length.BasicValueType));

            return(Append(new NewView(
                              GetInitializer(location),
                              pointer,
                              length)));
        }
Пример #9
0
        /// <summary>
        /// Computes a new sub view from a given view.
        /// </summary>
        /// <param name="location">The current location.</param>
        /// <param name="source">The source.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="length">The length.</param>
        /// <returns>A node that represents the new sub view.</returns>
        public ValueReference CreateSubViewValue(
            Location location,
            Value source,
            Value offset,
            Value length)
        {
            location.Assert(
                source.Type.IsViewType &&
                IRTypeContext.IsViewIndexType(offset.BasicValueType) &&
                IRTypeContext.IsViewIndexType(length.BasicValueType));

            return(Append(new SubViewValue(
                              GetInitializer(location),
                              source,
                              offset,
                              length)));
        }
Пример #10
0
        /// <summary>
        /// Constructs a new type generator and defines all required types
        /// in OpenCL during construction.
        /// </summary>
        /// <param name="typeContext">The associated type context.</param>
        internal CLTypeGenerator(IRTypeContext typeContext)
        {
            TypeContext = typeContext;

            // Declare primitive types
            mapping[typeContext.VoidType]   = "void";
            mapping[typeContext.StringType] = "char*";

            foreach (var basicValueType in IRTypeContext.BasicValueTypes)
            {
                if (basicValueType == BasicValueType.Float64 && TypeContext.Context.HasFlags(ContextFlags.Force32BitFloats))
                {
                    continue;
                }
                var primitiveType = typeContext.GetPrimitiveType(basicValueType);
                mapping[primitiveType] = GetBasicValueType(basicValueType);
            }
        }
Пример #11
0
        /// <summary>
        /// Constructs a new ABI specification.
        /// </summary>
        /// <param name="typeContext">The parent type context.</param>
        /// <param name="targetPlatform">The target platform</param>
        /// <param name="viewTypeInfoProvider">The ABI info object provider for a view.</param>
        protected ABI(
            IRTypeContext typeContext,
            TargetPlatform targetPlatform,
            Func <int, ABITypeInfo> viewTypeInfoProvider)
        {
            Debug.Assert(typeContext != null, "Invalid type context");
            TypeContext    = typeContext;
            TargetPlatform = targetPlatform;

            PointerArithmeticType = targetPlatform == TargetPlatform.X64 ?
                                    ArithmeticBasicValueType.UInt64 :
                                    ArithmeticBasicValueType.UInt32;
            PointerType = typeContext.GetPrimitiveType(
                PointerArithmeticType.GetBasicValueType());
            PointerSize = GetSizeOf(PointerType);

            ViewTypeInfo = viewTypeInfoProvider(PointerSize);
        }
Пример #12
0
        /// <summary>
        /// Constructs a new type generator and defines all required types
        /// in OpenCL during construction.
        /// </summary>
        /// <param name="scopeProvider">All relevant method scopes.</param>
        /// <param name="typeContext">The associated type context.</param>
        /// <param name="target">The target builder to write to.</param>
        internal CLTypeGenerator(
            CachedScopeProvider scopeProvider,
            IRTypeContext typeContext,
            StringBuilder target)
        {
            Builder = target;

            // Declare primitive types
            mapping[typeContext.VoidType]   = "void";
            mapping[typeContext.StringType] = "char*";

            foreach (var basicValueType in IRTypeContext.BasicValueTypes)
            {
                var primitiveType = typeContext.GetPrimitiveType(basicValueType);
                mapping[primitiveType] = GetBasicValueType(basicValueType);
            }

            // Generate all type declarations
            foreach (var(_, scope) in scopeProvider)
            {
                // Check all node types
                foreach (Value value in scope.Values)
                {
                    if (!mapping.ContainsKey(value.Type))
                    {
                        DeclareType(value.Type);
                    }
                }
            }

            // Generate all pointer types
            foreach (var typeNode in mapping.Keys)
            {
                DefinePointerType(typeNode);
            }

            // Generate all structure types
            var typeVisitor = new TypeVisitor(this);

            foreach (var typeNode in mapping.Keys)
            {
                typeNode.Accept(typeVisitor);
            }
        }
Пример #13
0
 /// <summary>
 /// Constructs a new object type.
 /// </summary>
 /// <param name="typeContext">The parent type context.</param>
 protected ObjectType(IRTypeContext typeContext)
     : base(typeContext)
 {
 }
Пример #14
0
 /// <summary>
 /// Constructs a new IL ABI
 /// </summary>
 /// <param name="typeContext">The current type context.</param>
 /// <param name="targetPlatform">The target platform</param>
 public ILABI(
     IRTypeContext typeContext,
     TargetPlatform targetPlatform)
     : base(typeContext, targetPlatform)
 {
 }
Пример #15
0
        /// <summary>
        /// Constructs a new type lowering.
        /// </summary>
        /// <param name="typeContext">The parent type context.</param>
        protected TypeLowering(IRTypeContext typeContext)
        {
            Debug.Assert(typeContext != null, "Invalid type context");

            TypeContext = typeContext;
        }
Пример #16
0
        /// <summary>
        /// Constructs a new ILGPU main context
        /// </summary>
        /// <param name="builder">The parent builder instance.</param>
        /// <param name="devices">The array of accelerator descriptions.</param>
        internal Context(
            Builder builder,
            ImmutableArray <Device> devices)
        {
            InstanceId     = InstanceId.CreateNew();
            TargetPlatform = Backend.RuntimePlatform;
            RuntimeSystem  = new RuntimeSystem();
            Properties     = builder.InstantiateProperties();

            // Initialize verifier
            Verifier = builder.EnableVerifier ? Verifier.Instance : Verifier.Empty;

            // Initialize main contexts
            TypeContext = new IRTypeContext(this);
            IRContext   = new IRContext(this);

            // Initialize intrinsic manager
            IntrinsicManager = builder.IntrinsicManager;

            // Create frontend
            DebugInformationManager frontendDebugInformationManager =
                Properties.DebugSymbolsMode > DebugSymbolsMode.Disabled
                ? DebugInformationManager
                : null;

            ILFrontend = builder.EnableParallelCodeGenerationInFrontend
                ? new ILFrontend(this, frontendDebugInformationManager)
                : new ILFrontend(this, frontendDebugInformationManager, 1);

            // Create default IL backend
            DefautltILBackend = new DefaultILBackend(this);

            // Initialize default transformer
            ContextTransformer = Optimizer.CreateTransformer(
                Properties.OptimizationLevel,
                TransformerConfiguration.Transformed,
                Properties.InliningMode);

            // Initialize the default CPU device
            // NB: Ensure that the current accelerator is not changed when creating the
            // implicit CPU accelerator. Otherwise, we may unintentionally initialize
            // Accelerator.Current before an accelerator has been created by the user.
            var currentAccelerator = Accelerator.Current;

            CPUAccelerator = new CPUAccelerator(
                this,
                CPUDevice.Implicit,
                CPUAcceleratorMode.Parallel,
                ThreadPriority.Lowest);
            Debug.Assert(Accelerator.Current == currentAccelerator);

            // Initialize all devices
            Devices = devices;
            if (devices.IsDefaultOrEmpty)
            {
                // Add a default CPU device
                Devices = ImmutableArray.Create <Device>(CPUDevice.Default);
            }

            // Create a mapping
            deviceMapping = new Dictionary <AcceleratorType, List <Device> >(Devices.Length);
            foreach (var device in Devices)
            {
                if (!deviceMapping.TryGetValue(device.AcceleratorType, out var devs))
                {
                    devs = new List <Device>(8);
                    deviceMapping.Add(device.AcceleratorType, devs);
                }
                devs.Add(device);
            }
        }
Пример #17
0
 /// <summary>
 /// Constructs a new type lowering.
 /// </summary>
 /// <param name="typeContext">The parent type context.</param>
 protected ViewTypeLowering(IRTypeContext typeContext)
     : base(typeContext)
 {
 }