예제 #1
0
        /// <summary>
        /// Adds general backend optimizations.
        /// </summary>
        /// <param name="builder">The transformation manager to populate.</param>
        /// <param name="acceleratorSpecializer">
        /// An instance of an <see cref="AcceleratorSpecializer"/> class.
        /// </param>
        /// <param name="level">The desired optimization level.</param>
        public static void AddBackendOptimizations(
            this Transformer.Builder builder,
            AcceleratorSpecializer acceleratorSpecializer,
            OptimizationLevel level)
        {
            // Specialize accelerator properties and views
            builder.Add(new LowerArrays());
            builder.Add(new LowerPointerViews());
            builder.Add(acceleratorSpecializer);

            // Lower structures
            if (level > OptimizationLevel.O1)
            {
                builder.Add(new LowerStructures());
            }

            // Apply final DCE phase in release mode
            if (level > OptimizationLevel.O0)
            {
                builder.Add(new DeadCodeElimination());
            }

            // Append further backend specific transformations in release mode
            builder.Add(new Inliner());
            if (level > OptimizationLevel.O0)
            {
                builder.Add(new SimplifyControlFlow());
            }
        }
예제 #2
0
 /// <summary>
 /// Populates the given transformation manager with O0 optimizations.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 /// <param name="inliningMode">The inlining mode to use.</param>
 public static void AddO0Optimizations(
     this Transformer.Builder builder,
     InliningMode inliningMode)
 {
     builder.AddBasicOptimizations(inliningMode);
     builder.AddAddressSpaceOptimizations();
 }
예제 #3
0
 /// <summary>
 /// Populates the given transformation manager with O0 optimizations.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 /// <param name="contextFlags">The context flags.</param>
 public static void AddO0Optimizations(
     this Transformer.Builder builder,
     ContextFlags contextFlags)
 {
     builder.AddBasicOptimizations(contextFlags);
     builder.AddAddressSpaceOptimizations();
 }
예제 #4
0
        /// <summary>
        /// Adds general backend optimizations.
        /// </summary>
        /// <param name="builder">The transformation manager to populate.</param>
        /// <param name="acceleratorSpecializer">
        /// An instance of an <see cref="AcceleratorSpecializer"/> class.
        /// </param>
        /// <param name="inliningMode">The inlining mode to use.</param>
        /// <param name="level">The desired optimization level.</param>
        public static void AddBackendOptimizations(
            this Transformer.Builder builder,
            AcceleratorSpecializer acceleratorSpecializer,
            InliningMode inliningMode,
            OptimizationLevel level)
        {
            // Specialize accelerator properties, arrays and views
            builder.Add(new LowerArrays(MemoryAddressSpace.Local));
            builder.Add(new LowerPointerViews());
            builder.Add(acceleratorSpecializer);

            // Perform an additional inlining pass to specialize small device-specific
            // functions that could have been introduced
            if (inliningMode != InliningMode.Disabled)
            {
                builder.Add(new Inliner());
            }

            // Skip further optimizations in debug mode
            if (level < OptimizationLevel.O1)
            {
                return;
            }

            // Use experimental address-space specializer in O2 only
            if (level > OptimizationLevel.O1)
            {
                // Specialize all parameter address spaces
                builder.Add(new InferKernelAddressSpaces(MemoryAddressSpace.Global));
            }

            // Lower all value structures that could have been created during the
            // following passes:
            // LowerArrays, LowerPointerViews, AcceleratorSpecializer and
            // AddressSpaceSpecializer
            builder.Add(new LowerStructures());

            // Apply DCE phase in release mode to remove all dead values that
            // could be created in prior passes
            builder.Add(new DeadCodeElimination());

            // Converts local memory arrays into compile-time known structures
            builder.Add(new SSAStructureConstruction());

            // Infer all specialized address spaces
            if (level > OptimizationLevel.O1)
            {
                builder.Add(new InferLocalAddressSpaces());
            }
            else
            {
                builder.Add(new InferAddressSpaces());
            }

            // Final cleanup phases to improve performance
            builder.Add(new CleanupBlocks());
            builder.Add(new SimplifyControlFlow());
        }
예제 #5
0
 /// <summary>
 /// Populates the given transformation manager with O0 optimizations.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 /// <param name="contextFlags">The context flags.</param>
 public static void AddO0Optimizations(
     this Transformer.Builder builder,
     ContextFlags contextFlags)
 {
     builder.AddBasicOptimizations(contextFlags);
     builder.Add(new SSAConstruction());
     builder.Add(new DeadCodeElimination());
     builder.Add(new InferAddressSpaces());
 }
예제 #6
0
 /// <summary>
 /// Adds loop-specific optimizations.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 /// <remarks>
 /// Loop-invariant code will be moved out of loops, loops with a known trip
 /// count will be unrolled and (potentially new) unreachable code will be
 /// removed.
 /// </remarks>
 public static void AddLoopOptimizations(
     this Transformer.Builder builder)
 {
     builder.Add(new LoopInvariantCodeMotion());
     builder.Add(new LoopUnrolling());
     builder.Add(new UnreachableCodeElimination());
     builder.Add(new DeadCodeElimination());
     builder.Add(new SimplifyControlFlow());
 }
예제 #7
0
 /// <summary>
 /// Adds basic optimization transformations.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 /// <param name="contextFlags">The context flags.</param>
 public static void AddBasicOptimizations(
     this Transformer.Builder builder,
     ContextFlags contextFlags)
 {
     if (!contextFlags.HasFlags(ContextFlags.NoInlining))
     {
         builder.Add(new Inliner());
     }
     builder.Add(new SimplifyControlFlow());
 }
예제 #8
0
 /// <summary>
 /// Populates the given transformation manager with O1 optimizations.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 /// <param name="contextFlags">The context flags.</param>
 public static void AddO1Optimizations(
     this Transformer.Builder builder,
     ContextFlags contextFlags)
 {
     builder.AddBasicOptimizations(contextFlags);
     builder.AddStructureOptimizations();
     builder.AddLoopOptimizations();
     builder.AddConditionalOptimizations();
     builder.AddAddressSpaceOptimizations();
 }
예제 #9
0
 /// <summary>
 /// Populates the given transformation manager with O1 optimizations.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 /// <param name="inliningMode">The inlining mode to use.</param>
 public static void AddO1Optimizations(
     this Transformer.Builder builder,
     InliningMode inliningMode)
 {
     builder.AddBasicOptimizations(inliningMode);
     builder.AddStructureOptimizations();
     builder.AddLoopOptimizations();
     builder.AddConditionalOptimizations();
     builder.AddAddressSpaceOptimizations();
 }
예제 #10
0
 /// <summary>
 /// Populates the given transformation manager with the required
 /// optimization transformations.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 /// <param name="contextFlags">The context flags.</param>
 /// <param name="level">The desired optimization level.</param>
 /// <returns>The maximum number of iterations.</returns>
 public static void AddOptimizations(
     this Transformer.Builder builder,
     ContextFlags contextFlags,
     OptimizationLevel level)
 {
     if (level < OptimizationLevel.O0 || level > OptimizationLevel.O2)
     {
         throw new ArgumentOutOfRangeException(nameof(level));
     }
     OptimizationHandlers[(int)level](builder, contextFlags);
 }
예제 #11
0
 /// <summary>
 /// Populates the given transformation manager with the required
 /// optimization transformations.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 /// <param name="inliningMode">The inlining mode to use.</param>
 /// <param name="level">The desired optimization level.</param>
 /// <returns>The maximum number of iterations.</returns>
 public static void AddOptimizations(
     this Transformer.Builder builder,
     InliningMode inliningMode,
     OptimizationLevel level)
 {
     if (level < OptimizationLevel.O0 || level > OptimizationLevel.O2)
     {
         throw new ArgumentOutOfRangeException(nameof(level));
     }
     OptimizationHandlers[(int)level](builder, inliningMode);
 }
예제 #12
0
 /// <summary>
 /// Populates the given transformation manager with the required
 /// debug optimization transformations.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 /// <param name="contextFlags">The context flags.</param>
 public static void AddDebugOptimizations(
     this Transformer.Builder builder,
     ContextFlags contextFlags)
 {
     if (!contextFlags.HasFlags(ContextFlags.NoInlining))
     {
         builder.Add(new Inliner());
     }
     builder.Add(new SimplifyControlFlow());
     builder.Add(new SSAConstruction());
     builder.Add(new DeadCodeElimination());
 }
예제 #13
0
 /// <summary>
 /// Adds basic optimization transformations.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 /// <param name="inliningMode">The inlining mode to use.</param>
 public static void AddBasicOptimizations(
     this Transformer.Builder builder,
     InliningMode inliningMode)
 {
     if (inliningMode != InliningMode.Disabled)
     {
         builder.Add(new Inliner());
     }
     builder.Add(new SimplifyControlFlow());
     builder.Add(new SSAConstruction());
     builder.Add(new DeadCodeElimination());
 }
예제 #14
0
        /// <summary>
        /// Populates the given transformation manager with O2 optimizations.
        /// </summary>
        /// <param name="builder">The transformation manager to populate.</param>
        /// <param name="inliningMode">The inlining mode to use.</param>
        public static void AddO2Optimizations(
            this Transformer.Builder builder,
            InliningMode inliningMode)
        {
            builder.AddBasicOptimizations(inliningMode);
            builder.AddStructureOptimizations();
            builder.AddLoopOptimizations();

            // Append experimental if-condition conversion pass
            builder.Add(new IfConditionConversion());
            // Remove all temporarily generated values that are no longer required
            builder.Add(new DeadCodeElimination());

            builder.AddConditionalOptimizations();
            builder.AddAddressSpaceOptimizations();
        }
예제 #15
0
        /// <summary>
        /// Populates the given transformation manager with O2 optimizations.
        /// </summary>
        /// <param name="builder">The transformation manager to populate.</param>
        /// <param name="contextFlags">The context flags.</param>
        public static void AddO2Optimizations(
            this Transformer.Builder builder,
            ContextFlags contextFlags)
        {
            var dce = new DeadCodeElimination();

            builder.AddBasicOptimizations(contextFlags);
            builder.Add(dce);
            builder.Add(new SSAConstruction());
            builder.Add(new LowerStructures());
            builder.Add(dce);
            builder.Add(new IfConversion(IfConversionFlags.Default));
            builder.Add(dce);
            builder.Add(new SimplifyControlFlow());
            builder.Add(new InferAddressSpaces());
        }
예제 #16
0
        /// <summary>
        /// Adds general backend optimizations.
        /// </summary>
        /// <param name="builder">The transformation manager to populate.</param>
        /// <param name="acceleratorSpecializer">
        /// An instance of an <see cref="AcceleratorSpecializer"/> class.
        /// </param>
        /// <param name="level">The desired optimization level.</param>
        public static void AddBackendOptimizations(
            this Transformer.Builder builder,
            AcceleratorSpecializer acceleratorSpecializer,
            OptimizationLevel level)
        {
            // Specialize accelerator properties and views
            builder.Add(new LowerArrays());
            builder.Add(new LowerPointerViews());
            builder.Add(acceleratorSpecializer);

            // Specialize all parameter address spaces
            if (level > OptimizationLevel.O1)
            {
                builder.Add(new AddressSpaceSpecializer(MemoryAddressSpace.Global));
            }

            // Lower structures
            if (level > OptimizationLevel.O1)
            {
                builder.Add(new LowerStructures());
            }

            // Apply DCE phase in release mode
            if (level > OptimizationLevel.O0)
            {
                builder.Add(new DeadCodeElimination());
            }

            // Infer all specialized address spaces
            if (level > OptimizationLevel.O1)
            {
                builder.Add(new InferAddressSpaces());
            }

            // Append further backend specific transformations in O2 and O1 mode
            builder.Add(new Inliner());
            if (level > OptimizationLevel.O1)
            {
                builder.Add(new CleanupBlocks());
                builder.Add(new SimplifyControlFlow());
                builder.Add(new DeadCodeElimination());
            }
            else if (level > OptimizationLevel.O0)
            {
                builder.Add(new SimplifyControlFlow());
            }
        }
예제 #17
0
        /// <summary>
        /// Adds general backend optimizations.
        /// </summary>
        /// <param name="builder">The transformation manager to populate.</param>
        /// <param name="acceleratorSpecializer">
        /// An instance of an <see cref="AcceleratorSpecializer"/> class.
        /// </param>
        /// <param name="contextFlags">The context flags.</param>
        /// <param name="level">The desired optimization level.</param>
        public static void AddBackendOptimizations(
            this Transformer.Builder builder,
            AcceleratorSpecializer acceleratorSpecializer,
            ContextFlags contextFlags,
            OptimizationLevel level)
        {
            // Specialize accelerator properties and views
            builder.Add(new LowerPointerViews());
            builder.Add(acceleratorSpecializer);

            // Perform an additional inlining pass to specialize small device-specific
            // functions that could have been introduced
            if (!contextFlags.HasFlags(ContextFlags.NoInlining))
            {
                builder.Add(new Inliner());
            }

            // Skip further optimizations in debug mode
            if (level < OptimizationLevel.O1)
            {
                return;
            }

            // Use experimental address-space specializer in O2 only
            if (level > OptimizationLevel.O1)
            {
                // Specialize all parameter address spaces
                builder.Add(new AddressSpaceSpecializer(MemoryAddressSpace.Global));
            }

            // Lower all value structures that could have been created during the
            // following passes:
            // LowerPointerViews, AcceleratorSpecializer and AddressSpaceSpecializer
            builder.Add(new LowerStructures());

            // Apply DCE phase in release mode to remove all dead values that
            // could be created in prior passes
            builder.Add(new DeadCodeElimination());

            // Infer all specialized address spaces
            builder.Add(new InferAddressSpaces());

            // Final cleanup phases to improve performance
            builder.Add(new CleanupBlocks());
            builder.Add(new SimplifyControlFlow());
        }
예제 #18
0
파일: Optimizer.cs 프로젝트: Costigan/ILGPU
        /// <summary>
        /// Adds general backend optimizations.
        /// </summary>
        /// <param name="builder">The transformation manager to populate.</param>
        /// <param name="acceleratorSpecializer">
        /// An instance of an <see cref="AcceleratorSpecializer"/> class.
        /// </param>
        /// <param name="level">The desired optimization level.</param>
        public static void AddBackendOptimizations(
            this Transformer.Builder builder,
            AcceleratorSpecializer acceleratorSpecializer,
            OptimizationLevel level)
        {
            // Specialize accelerator properties and views
            builder.Add(new LowerArrays());
            builder.Add(new LowerPointerViews());
            builder.Add(acceleratorSpecializer);

            // Lower structures
            if (level > OptimizationLevel.O1)
            {
                builder.Add(new LowerStructures());
                builder.Add(new DeadCodeElimination());
            }
        }
예제 #19
0
        /// <summary>
        /// Populates the given transformation manager with O2 optimizations.
        /// </summary>
        /// <param name="builder">The transformation manager to populate.</param>
        /// <param name="contextFlags">The context flags.</param>
        public static void AddO2Optimizations(
            this Transformer.Builder builder,
            ContextFlags contextFlags)
        {
            builder.AddBasicOptimizations(contextFlags);
            builder.AddStructureOptimizations();
            builder.AddLoopOptimizations();

            // Converts local memory arrays into structure values
            builder.Add(new SSAStructureConstruction());
            // Append experimental if-condition conversion pass
            builder.Add(new IfConditionConversion());
            // Remove all temporarily generated values that are no longer required
            builder.Add(new DeadCodeElimination());

            builder.AddConditionalOptimizations();
            builder.AddAddressSpaceOptimizations();
        }
예제 #20
0
        public static void AddOptimizations(
            this Transformer.Builder builder,
            ContextFlags contextFlags,
            OptimizationLevel level)
        {
            switch (level)
            {
            case OptimizationLevel.Debug:
                AddDebugOptimizations(builder, contextFlags);
                break;

            case OptimizationLevel.Release:
                AddReleaseOptimizations(builder, contextFlags);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(level));
            }
        }
예제 #21
0
 /// <summary>
 /// Adds structure optimization passes that lower and remove structure values.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 /// <remarks>
 /// Helps to reduce register pressure and to avoid unnecessary allocations.
 /// </remarks>
 public static void AddStructureOptimizations(
     this Transformer.Builder builder)
 {
     builder.Add(new LowerStructures());
     builder.Add(new DeadCodeElimination());
 }
예제 #22
0
        /// <summary>
        /// Adds general backend optimizations.
        /// </summary>
        /// <param name="builder">The transformation manager to populate.</param>
        /// <param name="acceleratorSpecializer">
        /// An instance of an <see cref="AcceleratorSpecializer"/> class.
        /// </param>
        /// <param name="inliningMode">The inlining mode to use.</param>
        /// <param name="level">The desired optimization level.</param>
        public static void AddBackendOptimizations <TPlacementStrategy>(
            this Transformer.Builder builder,
            AcceleratorSpecializer acceleratorSpecializer,
            InliningMode inliningMode,
            OptimizationLevel level)
            where TPlacementStrategy : struct, CodePlacement.IPlacementStrategy
        {
            // Specialize accelerator properties, arrays and views
            builder.Add(new LowerArrays(MemoryAddressSpace.Local));
            builder.Add(new LowerPointerViews());
            builder.Add(acceleratorSpecializer);

            // Perform an additional inlining pass to specialize small device-specific
            // functions that could have been introduced
            if (inliningMode != InliningMode.Disabled)
            {
                builder.Add(new Inliner());
            }

            // Apply UCE and DCE passes to avoid dead branches and fold conditionals that
            // do not affect the actual code being executed
            builder.Add(new UnreachableCodeElimination());
            builder.Add(new DeadCodeElimination());

            // Skip further optimizations in debug mode
            if (level < OptimizationLevel.O1)
            {
                return;
            }

            // Use experimental address-space specializer in O2 only
            if (level > OptimizationLevel.O1)
            {
                // Specialize all parameter address spaces
                builder.Add(new InferKernelAddressSpaces(MemoryAddressSpace.Global));
            }

            // Lower all value structures that could have been created during the
            // following passes:
            // LowerArrays, LowerPointerViews, AcceleratorSpecializer and
            // AddressSpaceSpecializer
            builder.Add(new LowerStructures());

            // Apply UCE and DCE phases in release mode to remove all dead values and
            // branches that could be have been created in prior passes
            builder.Add(new UnreachableCodeElimination());
            builder.Add(new DeadCodeElimination());

            // Converts local memory arrays into compile-time known structures
            builder.Add(new SSAStructureConstruction());

            // Infer all specialized address spaces
            if (level > OptimizationLevel.O1)
            {
                builder.Add(new InferLocalAddressSpaces());
            }
            else
            {
                builder.Add(new InferAddressSpaces());
            }

            // Final cleanup phases to improve performance
            builder.Add(new CleanupBlocks());
            builder.Add(new SimplifyControlFlow());

            if (level > OptimizationLevel.O1)
            {
                // Add additional code placement optimizations to reduce register
                // pressure and improve performance
                builder.Add(new DeadCodeElimination());
                builder.Add(new CodePlacement <TPlacementStrategy>(
                                CodePlacementMode.Aggressive));
                builder.Add(new LoopInvariantCodeMotion());
            }
        }
예제 #23
0
 /// <summary>
 /// Adds optimizations passes to convert control-flow ifs into fast predicates.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 public static void AddConditionalOptimizations(
     this Transformer.Builder builder)
 {
     builder.Add(new IfConversion());
     builder.Add(new SimplifyControlFlow());
 }
예제 #24
0
 /// <summary>
 /// Adds address-space operation optimizations.
 /// </summary>
 /// <param name="builder">The transformation manager to populate.</param>
 /// <remarks>
 /// Converts operations working on the generic address space into operations
 /// working on specific address spaces to improve performance.
 /// </remarks>
 public static void AddAddressSpaceOptimizations(
     this Transformer.Builder builder) =>
 builder.Add(new InferAddressSpaces());