Skip to content

Commit 88a05e0

Browse files
committed
llvm commands
1 parent 6e5eb41 commit 88a05e0

File tree

14 files changed

+12322
-22
lines changed

14 files changed

+12322
-22
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

Dockerfile

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,11 @@ RUN apt-get -y install git
1212
RUN apt-get -y install libreoffice
1313
RUN apt-get -y install curl
1414
RUN apt-get -y install gnupg
15+
RUN apt-get -y install gifsicle
1516
RUN npm install -g pnpm
16-
17-
# LLVM prereqs
18-
RUN apt-get -qq update; \
19-
apt-get install -qqy --no-install-recommends \
20-
gnupg2 wget ca-certificates apt-transport-https \
21-
autoconf automake cmake dpkg-dev file make patch libc6-dev
22-
23-
# Install LLVM
24-
COPY llvm.txt .
25-
RUN cat llvm.txt > /etc/apt/sources.list.d/llvm.list && \
26-
wget -qO /etc/apt/trusted.gpg.d/llvm.asc \
27-
https://apt.llvm.org/llvm-snapshot.gpg.key && \
28-
apt-get -qq update && \
29-
apt-get install -qqy -t bullseye clang-17 clang-tidy-17 clang-format-17 libc++-17-dev libc++abi-17-dev && \
30-
for f in /usr/lib/llvm-*/bin/*; do ln -sf "$f" /usr/bin; done && \
31-
ln -sf clang /usr/bin/cc && \
32-
ln -sf clang /usr/bin/c89 && \
33-
ln -sf clang /usr/bin/c99 && \
34-
ln -sf clang++ /usr/bin/c++ && \
35-
ln -sf clang++ /usr/bin/g++ && \
36-
rm -rf /var/lib/apt/lists/*
17+
RUN apt-get -y install llvm
18+
RUN apt-get -y install clang
19+
RUN apt-get -y install clang-format
3720

3821
LABEL org.opencontainers.image.source https://github.com/termsurf/task
3922
LABEL org.opencontainers.image.title "Task: Common Actions Interface"

code/node/code.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import {
2+
LLVM_ARCHITECTURE_CONTENT,
3+
LlvmArchitecture,
4+
} from '../shared/code'
5+
import { IOPath } from '../shared/type'
6+
import { exec } from './process'
7+
8+
export const LLVM_OPTIMIZATION_LEVEL = [0, 1, 2, 3]
9+
10+
export type LlvmOptimizationLevel =
11+
(typeof LLVM_OPTIMIZATION_LEVEL)[number]
12+
13+
// ; ModuleID = 'tmp/c.c'
14+
// source_filename = "tmp/c.c"
15+
// target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
16+
// target triple = "arm64-apple-macosx14.0.0"
17+
// @.str = private unnamed_addr constant [14 x i8] c"Hello, World!\00", align 1
18+
export async function generateLlvmIrFromC({
19+
inputPath,
20+
outputPath,
21+
optimizationLevel = 0,
22+
fastMath = false,
23+
}: IOPath & {
24+
optimizationLevel?: LlvmOptimizationLevel
25+
fastMath?: boolean
26+
}) {
27+
const cmd = [`clang -S -emit-llvm -O${optimizationLevel}`]
28+
29+
if (fastMath) {
30+
cmd.push(`-ffast-math`)
31+
}
32+
33+
cmd.push(`-o "${outputPath}" "${inputPath}"`)
34+
35+
return await exec(cmd.join(' '))
36+
}
37+
38+
export async function generateLlvmIrFromCPP({
39+
inputPath,
40+
outputPath,
41+
optimizationLevel = 0,
42+
fastMath = false,
43+
}: IOPath & {
44+
optimizationLevel?: LlvmOptimizationLevel
45+
fastMath?: boolean
46+
}) {
47+
const cmd = [`clang++ -S -emit-llvm -O${optimizationLevel}`]
48+
49+
if (fastMath) {
50+
cmd.push(`-ffast-math`)
51+
}
52+
53+
cmd.push(`-o "${outputPath}" "${inputPath}"`)
54+
55+
return await exec(cmd.join(' '))
56+
}
57+
58+
export const ASSEMBLY_SYNTAX = ['intel', 'att']
59+
60+
export type AssemblySyntax = (typeof ASSEMBLY_SYNTAX)[number]
61+
62+
export async function generateAssemblyFromLlvmIr({
63+
inputPath,
64+
outputPath,
65+
syntax = 'intel',
66+
architecture = 'x86_64',
67+
}: IOPath & {
68+
syntax: AssemblySyntax
69+
architecture: LlvmArchitecture
70+
}) {
71+
const architectureKey = LLVM_ARCHITECTURE_CONTENT[architecture].key
72+
73+
return await exec(
74+
`llc --x86-asm-syntax=${syntax} -march=${architectureKey} -o "${outputPath}" "${inputPath}"`,
75+
)
76+
}

code/node/image.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,7 @@ export async function convertImageWithImageMagick({
104104
// height: meta.height,
105105
// }
106106
// }
107+
108+
// optimize animated gif:
109+
// gifsicle -O3 --colors 256 --lossy=30 -o output.gif input.gif
110+
// pix_fmt rgb24

code/node/video.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ export async function convertVideoToAudioWithFFmpeg({
4545
return await exec(`ffmpeg -i "${inputPath}" -vn ${outputPath}`)
4646
}
4747

48+
export async function removeAudioFromVideoWithFFmpeg({
49+
inputPath,
50+
outputPath,
51+
}: IOPath) {
52+
// output.mp3
53+
return await exec(`ffmpeg -i "${inputPath}" -an ${outputPath}`)
54+
}
55+
4856
export async function compressMP4WithFFmpeg({
4957
inputPath,
5058
outputPath,
@@ -80,6 +88,36 @@ export async function compressMP4WithFFmpeg({
8088
// ffmpeg -i audio.ac3 -acodec libmp3lame audio.mp3
8189
// aac to mp3
8290
// ffmpeg -i audio.aac -acodec libmp3lame audio.mp3
91+
// crop: -filter:v "crop=w:h:x:y"
92+
// add audio to video:
93+
// ffmpeg -i audio.mp3 -i video.mp4 video_audio_mix.mkv
94+
// concat videos
95+
// ffmpeg -f concat -i file-list.txt -c copy output.mp4
96+
// speed up video
97+
// ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" output.mp4
98+
// compress video
99+
// ffmpeg -i input.mp4 -vf scale=1280:-1 -c:v libx264 -preset veryslow -crf 24 output.mp4
100+
// zoom in:
101+
// "scale=2*iw:-1, crop=iw/2:ih/2"
102+
// loop
103+
// Use -1 for an infinite loop.
104+
// ffmpeg -stream_loop 2 -i input.mp4 -c copy output.mp4
105+
// copy metadata
106+
// ffmpeg -i input.mov -map_metadata 0 -movflags use_metadata_tags output.mp4
107+
// add subtitles
108+
// convert image to video
109+
// ffmpeg -loop 1 -i input.png -c:v libx264 -t 30 -pix_fmt yuv420p video.mp4
110+
// change volume
111+
// ffmpeg -i input.mp3 -af 'volume=0.5' output.mp3
112+
// increase audio tempo
113+
// ffmpeg -i input.mp3 -filter:a "atempo=2.0" -vn output.mp3
114+
// add image to audio
115+
// ffmpeg -loop 1 -i image.jpg -i input.mp3 -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest output.mp4
116+
// encode for iphone
117+
// ffmpeg -i source_video.avi input -acodec aac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320x180 -title X final_video.mp4
118+
// extract sound from video
119+
// ffmpeg -i source_video.avi -vn -ar 44100 -ac 2 -ab 192k -f mp3 sound.mp3
120+
// https://catswhocode.com/ffmpeg-commands/
83121
export async function convertVideoWithFFmpeg({
84122
inputPath,
85123
outputPath,

code/shared/code.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import _LLVM_ARCHITECTURE_CONTENT from './data/llvm.architecture.json'
2+
import LLVM_CPU_CONTENT from './data/llvm.cpu.json'
3+
import LLVM_CPU from './data/llvm.cpu.key.json'
4+
import LLVM_FEATURE_CONTENT from './data/llvm.feature.json'
5+
import LLVM_FEATURE from './data/llvm.feature.key.json'
6+
7+
export const LLVM_ARCHITECTURE = [
8+
'aarch64',
9+
'aarch64_32',
10+
'aarch64_be',
11+
'amdgcn',
12+
'arm',
13+
'arm64',
14+
'arm64_32',
15+
'armeb',
16+
'avr',
17+
'bpf',
18+
'bpfeb',
19+
'bpfel',
20+
'hexagon',
21+
'lanai',
22+
'mips',
23+
'mips64',
24+
'mips64el',
25+
'mipsel',
26+
'msp430',
27+
'nvptx',
28+
'nvptx64',
29+
'ppc32',
30+
'ppc64',
31+
'ppc64le',
32+
'r600',
33+
'riscv32',
34+
'riscv64',
35+
'sparc',
36+
'sparcel',
37+
'sparcv9',
38+
'systemz',
39+
'thumb',
40+
'thumbeb',
41+
'wasm32',
42+
'wasm64',
43+
'x86',
44+
'x86_64',
45+
'xcore',
46+
]
47+
48+
export type LlvmArchitecture = (typeof LLVM_ARCHITECTURE)[number]
49+
50+
export const LLVM_ARCHITECTURE_CONTENT: Record<
51+
LlvmArchitecture,
52+
{ key: string; description: string }
53+
> = {
54+
aarch64: { key: 'aarch64', description: 'AArch64 (little endian)' },
55+
aarch64_32: {
56+
key: 'aarch64_32',
57+
description: 'AArch64 (little endian ILP32)',
58+
},
59+
aarch64_be: {
60+
key: 'aarch64_be',
61+
description: 'AArch64 (big endian)',
62+
},
63+
amdgcn: { key: 'amdgcn', description: 'AMD GCN GPUs' },
64+
arm: { key: 'arm', description: 'ARM' },
65+
arm64: { key: 'arm64', description: 'ARM64 (little endian)' },
66+
arm64_32: {
67+
key: 'arm64_32',
68+
description: 'ARM64 (little endian ILP32)',
69+
},
70+
armeb: { key: 'armeb', description: 'ARM (big endian)' },
71+
avr: { key: 'avr', description: 'Atmel AVR Microcontroller' },
72+
bpf: { key: 'bpf', description: 'BPF (host endian)' },
73+
bpfeb: { key: 'bpfeb', description: 'BPF (big endian)' },
74+
bpfel: { key: 'bpfel', description: 'BPF (little endian)' },
75+
hexagon: { key: 'hexagon', description: 'Hexagon' },
76+
lanai: { key: 'lanai', description: 'Lanai' },
77+
mips: { key: 'mips', description: 'MIPS (32-bit big endian)' },
78+
mips64: { key: 'mips64', description: 'MIPS (64-bit big endian)' },
79+
mips64el: {
80+
key: 'mips64el',
81+
description: 'MIPS (64-bit little endian)',
82+
},
83+
mipsel: { key: 'mipsel', description: 'MIPS (32-bit little endian)' },
84+
msp430: { key: 'msp430', description: 'MSP430 [experimental]' },
85+
nvptx: { key: 'nvptx', description: 'NVIDIA PTX 32-bit' },
86+
nvptx64: { key: 'nvptx64', description: 'NVIDIA PTX 64-bit' },
87+
ppc32: { key: 'ppc32', description: 'PowerPC 32' },
88+
ppc64: { key: 'ppc64', description: 'PowerPC 64' },
89+
ppc64le: { key: 'ppc64le', description: 'PowerPC 64 LE' },
90+
r600: { key: 'r600', description: 'AMD GPUs HD2XXX-HD6XXX' },
91+
riscv32: { key: 'riscv32', description: '32-bit RISC-V' },
92+
riscv64: { key: 'riscv64', description: '64-bit RISC-V' },
93+
sparc: { key: 'sparc', description: 'Sparc' },
94+
sparcel: { key: 'sparcel', description: 'Sparc LE' },
95+
sparcv9: { key: 'sparcv9', description: 'Sparc V9' },
96+
systemz: { key: 'systemz', description: 'SystemZ' },
97+
thumb: { key: 'thumb', description: 'Thumb' },
98+
thumbeb: { key: 'thumbeb', description: 'Thumb (big endian)' },
99+
wasm32: { key: 'wasm32', description: 'WebAssembly 32-bit' },
100+
wasm64: { key: 'wasm64', description: 'WebAssembly 64-bit' },
101+
x86: { key: 'x86', description: '32-bit X86: Pentium-Pro and above' },
102+
x86_64: { key: 'x86-64', description: '64-bit X86: EM64T and AMD64' },
103+
xcore: { key: 'xcore', description: 'XCore' },
104+
}
105+
106+
// export const LLVM_ARCHITECTURE_CONTENT_FEATURE: Record<LlvmArchitecture>
107+
108+
export { LLVM_CPU_CONTENT }
109+
export { LLVM_CPU }
110+
export { LLVM_FEATURE_CONTENT }
111+
export { LLVM_FEATURE }
112+
113+
export type LlvmCpu = (typeof LLVM_CPU)[number]
114+
export type LlvmFeature = (typeof LLVM_FEATURE)[number]
115+
116+
export const CLANG_FORMAT = [
117+
'llvm',
118+
'gnu',
119+
'google',
120+
'chromium',
121+
'microsoft',
122+
'mozilla',
123+
'webkit',
124+
]
125+
126+
export type ClangFormat = (typeof CLANG_FORMAT)[number]
127+
128+
export const CLANG_FORMAT_CONTENT: Record<
129+
ClangFormat,
130+
{ key: string }
131+
> = {
132+
llvm: { key: 'LLVM' },
133+
gnu: { key: 'GNU' },
134+
google: { key: 'Google' },
135+
chromium: { key: 'Chromium' },
136+
microsoft: { key: 'Microsoft' },
137+
mozilla: { key: 'Mozilla' },
138+
webkit: { key: 'WebKit' },
139+
}

0 commit comments

Comments
 (0)